Skip to content

Instantly share code, notes, and snippets.

@joshfeck
Created February 27, 2021 03:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshfeck/79e5e64826f1c769f557b5670c979217 to your computer and use it in GitHub Desktop.
Save joshfeck/79e5e64826f1c769f557b5670c979217 to your computer and use it in GitHub Desktop.
Exclude events from event lists using a tag. In this example, any events with the tag "exclude" will not be included in front end event lists. Event Espresso 4.
<?php // only add this opening PHP tag if starting with a new file with no opening PHP tag
add_filter( 'posts_where', 'ee_remove_hidden_tagged_event_list', 25, 2 );
function ee_remove_hidden_tagged_event_list( $SQL, WP_Query $wp_query ) {
global $wpdb;
$tag = get_term_by( 'slug', 'exclude', 'post_tag' );
if( $tag instanceof WP_Term ) {
$tag_id = $tag->term_id;
} else {
return $SQL;
}
if (
isset( $wp_query->query_vars['post_type'] ) &&
( $wp_query->query_vars['post_type'] == 'espresso_events' ||
( is_array( $wp_query->query_vars['post_type'] ) &&
in_array( 'espresso_events', $wp_query->query_vars['post_type'] ) ) ) &&
! $wp_query->is_singular ) {
$SQL .= " AND ID NOT IN (SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id={$tag_id} )"; // change to hidden tag ID
}
return $SQL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment