Exclude past events from search results

To exclude (The Events Calendar) past events from the default WordPress search, simply add the following snippet to your functions.php

				
					if ( ! function_exists( 'exclude_past_events_from_search' ) ) {
	function exclude_past_events_from_search( $query ) {
		if ($query->is_search) {
			$query->set( 'meta_query', array(
				'relation' => 'OR',
				array(
					'key' => '_EventEndDate',
					'value' => current_time( 'mysql' ),
					'compare' => '>',
					'type' => 'DATETIME',
				),
				array(
					'key' => '_EventEndDate',
					'compare' => 'NOT EXISTS',
				),
			) );
		}
		return $query;
	}
}

add_filter( 'pre_get_posts', 'exclude_past_events_from_search' );

				
			

Powered by BetterDocs

Newsletter

Subscribe and stay connected through our Newsletter. We send out important news, tips and special offers.

  • This field is for validation purposes and should be left unchanged.