Skip to content

Instantly share code, notes, and snippets.

@barryhughes
Created March 17, 2020 21:47
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 barryhughes/80423ba03c37d033e68aa52e8192daec to your computer and use it in GitHub Desktop.
Save barryhughes/80423ba03c37d033e68aa52e8192daec to your computer and use it in GitHub Desktop.
<?php
/**
* Remove events tagged with any of the slugs listed below from
* event views.
*
* We deliberately avoid use of TEC functions and constants etc
* so that this doesn't break should that plugin temporarily be
* deactivated.
*/
add_filter( 'pre_get_posts', function( $query ) {
if (
$query->get( 'post_type' ) !== 'tribe_events'
|| ! $query->is_archive()
) {
return;
}
// Add slugs of tags you wish to ignore here!
$tags = [
'ignore',
'hidden',
'missing',
# add more slugs here
];
// Let's convert them to tag IDs
foreach ( $tags as $index => $tag ) {
$term = get_term_by( 'slug', $tag, 'post_tag' );
if ( is_a( $term, 'WP_Term' ) ) {
$tags[$index] = $term->term_id;
}
else {
unset( $tags[$index] );
}
}
// No tag IDs? No need to do anything :-)
if ( empty( $tags ) ) {
return;
}
// Grab any existing tag IDs we should ignore and combine with our additional IDs.
$ignore_tags = (array) $query->get( 'tag__not_in' );
$ignore_tags = array_unique( array_merge( $ignore_tags, $tags ) );
// Update the query.
$query->set( 'tag__not_in', $ignore_tags );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment