Home Forums General Issues Hide past posts.


Hide past posts.

  • Hello and thanks for a great plugin. I probably should not post here since I have no knowledge of PHP, but I hope it’s not too much of a bother.

    I’ve created a custom post type called Event and then a custom field with date picker. With the help of a code example in the documentaion on here I was able to sort the events by their date from the date picker field. I added the code to functions.php

    function my_pre_get_posts( $query ) {
    	// do not modify queries in the admin
    	if( is_admin() ) {
    		return $query;
    	// only modify queries for 'event' post type
    	if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'event' ) {
    		$query->set('orderby', 'meta_value');	
    		$query->set('meta_key', 'start_date');	 
    		$query->set('order', 'ASC'); 
    	// return
    	return $query;
    add_action('pre_get_posts', 'my_pre_get_posts');

    I wonder if it would be possible to have a similar function for hiding past posts/events and only show current day and forward? When looking around I did find code that seemed to do this, but I was not able to change it so it would work or make it into a function.

    Thank you in advance.

    $query->set('meta_value', date('Ymd'));
    $query->set('meta_compare', '>=');

  • Thank you so much! That worked. I understand now I can add those query to the function. Thank you.

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

You must be logged in to reply to this topic.