Skip to content

Instantly share code, notes, and snippets.

@jmarchwinski
Last active July 21, 2020 15:01
Show Gist options
  • Save jmarchwinski/72418a6a2c3c48dea9bea6f974ac00d4 to your computer and use it in GitHub Desktop.
Save jmarchwinski/72418a6a2c3c48dea9bea6f974ac00d4 to your computer and use it in GitHub Desktop.
Hide past events from wordpress search
// Exclude past events from search
function mySearchFilter($query) {
if ($query->is_search) {
$past_events = tribe_get_events( array(
'posts_per_page' => -1,
'end_date' => new DateTime()
) );
$exclude_events = [];
foreach ( $past_events as $past_event ) {
$exclude_events[] = $past_event->ID;
}
$query->set('post__not_in', $exclude_events);
}
return $query;
}
add_filter('pre_get_posts','mySearchFilter');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment