Hide Past Events From Site Search
Removes events that have already ended from your site's search results, while keeping posts, pages and upcoming events, when The Events Calendar is active.
Opens WPIDE on
Code
add_action( 'pre_get_posts', function ( $query ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() || ! class_exists( 'Tribe__Events__Main' ) ) {
return;
}
$meta_query = (array) $query->get( 'meta_query' );
$meta_query[] = array(
'relation' => 'OR',
array( 'key' => '_EventEndDate', 'compare' => 'NOT EXISTS' ),
array( 'key' => '_EventEndDate', 'value' => current_time( 'mysql' ), 'compare' => '>=', 'type' => 'DATETIME' ),
);
$query->set( 'meta_query', $meta_query );
} );
Notes
Past events stay reachable through the calendar's own past-events view.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.