Support

Account

Home Forums General Issues Hide past posts.

Solved

Hide past posts.

    • Rinh

    • January 22, 2021 at 11:54 pm

    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', '>=');
    

    https://developer.wordpress.org/reference/classes/wp_query/

    • Rinh

    • January 23, 2021 at 12:37 am

    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.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.