Dear Support Team,
awhile ago I started a thread asking to exclude a certain category from all queries, but not for the administrator. I had found several topics but the snippets posted there did not seem to work for me: Whenever I used the list-view widget (TEC pro), all filters, even those set by the widget, were removed.
I have now managed to create a function that filters a specific category on all queries:
function tribe_exclude_events_category( $wp_query ) {
// only filter the query if the user cannot edit pages (< editor)
if (!current_user_can('edit_pages')) {
$exclude_cats = array('exclude-me');
// Join with current tax query if set
if (is_array($wp_query->get( 'tax_query')))
{$tax_query =$wp_query->get( 'tax_query');}
else
{$tax_query = array();}
// Setup an exclude from the tribe_events_cat taxonomy
$tax_query[] = array(
'taxonomy' => Tribe__Events__Main::TAXONOMY, // sidebar ist: "tribe_events_cat"
'field' => 'slug',
'terms' => $exclude_cats,
'operator' => 'NOT IN'
);
$wp_query->set('tax_query', $tax_query);
}
return $wp_query;
}
add_action( 'pre_get_posts', 'tribe_exclude_events_category', 10, 1 );
This is basically the code from this topic with a couple of changes. The most significant is, that the wrong tax_query was selected by the snippet in the other thread:
It must be $wp_query->get( 'tax_query') instead of $wp_query->tax_query
I just wanted to share this with the TEC community – maybe it will be helpful for others in the future.
Happy coding, everyone!