Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.