Exclude past events from search results

Home Forums Calendar Products Events Calendar PRO Exclude past events from search results

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1120839
    Meredith
    Participant

    I’ve googled this and not found answers, though it seems to be a pretty standard request. I’ve also tried implementing solutions, none of which have worked. Past events still always show in my search results.

    Seems that this should work, but it has no effect:

    add_action( 'pre_get_posts', 'edcny_pre_get_posts' );
    function edcny_pre_get_posts($query) {
    	if (!is_admin() && $query->is_main_query()) {
    		if ($query->is_search) {
    
    			$meta_query = $query->get('meta_query');
    			$meta_query[] = array(
    				'key' => '_EventStartDate',
    				'value' => date('Y-m-d').' 00:00:00',
    				'compare' => '>=',
    				'type' => 'DATETIME'
    			);
    			$query->set('meta_query', $meta_query);
    		}
    	}
    	return $query;
    }

    I’ve also tried replace the $meta_query section with this:
    $query->set('tribe_is_past', false);
    and then with this:
    $query->set('eventDisplay', 'upcoming');

    Those also don’t work. Where am I going wrong?

    Thanks,
    Aaron.

    #1121128
    Josh
    Participant

    Hey Aaron,

    Thanks for reaching out to us!

    To clarify, is this intended to be for a generic search when having your events selected to be displayed alongside your main blog posts? Or is this intended to impact the search associated with the bar that displays above the main event views within our plugin?

    Thanks!

    #1121177
    Meredith
    Participant

    Thanks for your quick reply, Josh.

    This is intended for the main search of the site. Example:
    http://cnyepiscopal.wpengine.com/?s=youth

    See, for example, in those search results “Registration Deadline: 2016 Youth Pilgrimage to El Salvador” which has the event date of May 15.

    Thanks!
    Aaron.

    #1121828
    Josh
    Participant

    Hey Meredith,

    Thanks for clarifying there.

    Taking a quick look at your code above, have you tried with changing the second layer of the conditional there to be:

    
    if( $query->is_search() )
    

    Thanks!

    #1122175
    Meredith
    Participant

    Josh, that’s not the problem. The code as I’ve written it (above) already does execute on the search results page. It just doesn’t actually filter out past events.

    #1123108
    Meredith
    Participant

    Checking in … Thanks.

    #1123328
    Josh
    Participant

    Hey Aaron,

    I apologize for the delay here. I’ve been playing with the snippet and trying several different approaches but haven’t been able to get search results to limit only to future events either.

    I’ll reach out to our team to see if anyone else has any input with a fresh set of eyes on it.

    Thanks!

    #1123538
    Meredith
    Participant

    Found the problem; my pre_get_posts modification was being ignored because of interference from another plugin (SearchWP).

    #1123795
    Josh
    Participant

    Hey Aaron,

    Awesome! I’m glad you were able to find a solution here.

    I’ll go ahead and close this thread for now. If you have any further questions, please don’t hesitate to open a new one.

    Thanks!

Viewing 9 posts - 1 through 9 (of 9 total)
  • The topic ‘Exclude past events from search results’ is closed to new replies.