Skip to content

Instantly share code, notes, and snippets.

@joshfeck
Created August 29, 2017 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshfeck/33a77ee2775e84658ff4955db5d5988e to your computer and use it in GitHub Desktop.
Save joshfeck/33a77ee2775e84658ff4955db5d5988e to your computer and use it in GitHub Desktop.
Exclude expired Event Espresso events from built-in WordPress search
<?php
//* Please do NOT include the opening php tag, except of course if you're starting with a blank file
add_filter('pre_get_posts','exclude_expired_events_from_search');
function exclude_expired_events_from_search($query) {
if (
$query instanceof WP_Query
&&
(
$query->is_search
&& $query->is_main_query()
)
) {
add_filter('posts_join', 'my_search_posts_join', 10, 2);
add_filter('posts_where', 'my_search_posts_where', 10, 2);
add_filter('posts_groupby', 'my_search_posts_groupby', 10, 2);
}
return $query;
}
function my_search_posts_join( $SQL, WP_Query $wp_query ) {
$SQL .= ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . EEM_Event::instance()->table() . '.ID = ' . EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name() . ' ) ';
return $SQL;
}
function my_search_posts_where( $SQL, WP_Query $wp_query ) {
$SQL .= ' AND ' . EEM_Datetime::instance()->table() . ".DTT_EVT_end > '" . current_time( 'mysql', true ) . "' ";
return $SQL;
}
function my_search_posts_groupby( $SQL, WP_Query $wp_query ) {
global $wpdb;
$SQL = $wpdb->posts . '.ID ';
return $SQL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment