1

I am looking to have a drop down menu at the top of the archive page or other pages with post loop that will allow the users to filter posts by date (year)

There will be a drop down with all the years posts are published. Eg: 2017, 2018, 2019

My Loop:

<?php
    $args = array(
        'cat' => $FeaturedNewsCat,
        'posts_per_page' => 2,
        'order' => DSC
    );
    $the_query = new WP_Query($args);
    while ( $the_query->have_posts() ) : $the_query->the_post();
?>

        <!--Post Template Comes Here-->

<?php
    endwhile;
    wp_reset_postdata();
?>

Please tell me what I have to do to implement the filter function. Any help is appreciated.

  • Have you tried Date Query? this codex Ref It seems this query checked the published date. – idpokute Jun 18 '18 at 19:26
1

I'm pretty sure you haven't even try to search for solution... If you did, then I'm pretty sure you would came to this Codex page, where you can find this code:

<select name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
  <option value=""><?php echo esc_attr( __( 'Select Month' ) ); ?></option> 
  <?php wp_get_archives( array( 'type' => 'monthly', 'format' => 'option', 'show_post_count' => 1 ) ); ?>
</select>

Which displays a drop-down box of monthly archives, in select tags, with the post count displayed.

All you have to do is to change 'monthly' to 'yearly' and start using archive template and global wp_query and not your own custom one.

  • Thank you! This works great. But there is one problem, when filtered it grabs posts from all categories. Is it possible to grab the posts only from the category that the user is viewing at the archive page? like if the user visited the following url: yoursite.com/?cat=2 he will only see posts from the category ID 2. But when filtered posts appear from all categories. – Web People Jun 19 '18 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.