Home Forums General Issues Custom year dropdown filter for CPT


Custom year dropdown filter for CPT

  • We have a custom post type (events) that has a custom date field (event_date). There is an overview page, where we show all dates paginated by 12, first pulling out upcoming events and then past events. That’s all working fine.

    What we want to do, is have a drop-down where visitors can filter by year, based on a custom date. We have a hard time coming up with a good way of doing this.

    Ideally we would be able to show results on their own URLs, eg etc, but this isn’t too important, we mainly just need a way to have a dropdown, which allows the user to get a filtered list of CPTs, based on custom year. In the dropdown, it would only show the years that there are CPTs with custom dates for.

    I’ve looked at tutorials like this one, but haven’t been able to use it to work with custom dates and dropdowns.

    Would be great if anyone would have an example for how to do this!

  • Hi @kaolarsan,

    Thanks for the post.

    The easiest way to go about this would be to create a custom query using the get_posts() function and filter the posts using the values entered in the datepicker field.

    Essentially, the code would look like so:

    $today = date('Ymd');
    $args = array (
        'post_type' => 'post',
        'meta_query' => array(
    	        'key'		=> 'start_date',
    	        'compare'	=> '>=',
    	        'value'		=> $today,
    	        'key'		=> 'end_date',
    	        'compare'	=> '<=',
    	        'value'		=> $today,
    // get posts
    $posts = get_posts($args);
  • Sorry @James, didn’t get to reply to this at the time. In the end we went with another solution, but I’ll be sure to revisit this solution in the future, when we need to build something similar. Many thanks for your reply!

  • Hi @kaolarsan,

    Thanks a lot for the feedback, this will surely also assist someone else in the same situation.

    If you get time to test it, let us know how it goes.

    Have a nice day 🙂

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Custom year dropdown filter for CPT’ is closed to new replies.