Vpp Man
Vpp Man

Reputation: 2546

Custom post type archive with date condition

Am using The7 Theme. Since I wanted to create a NEWS section to post latest news (apart from the Blog, which is separate), I used the Custom Post Type UI plugin to create a custom post type named news

I created a new file in my child theme, by copying the contents of the parent theme's archive.php file and named it as archive-news.php, with a minor modification:

<?php
/**
 * Archive pages.
 *
 * @since 1.0.0
 *
 * @package The7\Templates
 */

defined( 'ABSPATH' ) || exit;

$config = presscore_config();
$config->set( 'template', 'archive' );
$config->set( 'layout', 'masonry' );
$config->set( 'template.layout.type', 'masonry' );

get_header();
?>

    <!-- Content -->
    <div id="content" class="content" role="main">

        <div class="news_years text-center">
            <?php for( $i = date('Y'); $i >= 2009; $i-- ) { ?>
                <a href="#" class="news_year_btn"><?php echo $i; ?></a>
            <?php } ?>
        </div>

        <?php
        //the_archive_description( '<div class="taxonomy-description">', '</div>' );


        if ( have_posts() ) {
            the7_archive_loop();
        } else {
            get_template_part( 'no-results', 'search' );
        }
        ?>

    </div><!-- #content -->

    <?php do_action( 'presscore_after_content' ); ?>

<?php get_footer(); ?>

So the modification done was that I commented out the description that would be shown at the top of the page, and added year buttons.

After this, when the www.mysite.com/news was accessed, it pretty well loaded all my news items and when clicking an item, it loaded the detailed page as well. And the pagination also works very well, as it inherits the parents theme's template files.

The problem am facing is, I want to make it display year wise items also. Say for example, right now, when accessing the www.mysite.com/news url, it loads up all the News items with pagination. I want to filter it based on a particular year. That's why I included the additional FOR loop in the above code.

Upon searching, I found that the WP Query has date parameters that can be used which will resolve my issue.

But am not sure how to apply this as this archive page is already doing the fetching of posts. And also, how to pass the year in the URL as well. Any pointers would be highly appreciated.

If this is not the right forum to ask this question, please feel free to move this thread to the apt forum. Thank you.

Upvotes: 0

Views: 1200

Answers (0)

Related Questions