Excluding a Custom Post Type with a specific tag using pre_get_posts
-
29-09-2020 - |
Question
I have an archive page that lists a custom post type called 'event'. I have event posts with the tag 'regular', (the tag has an id of 53). I'd like to exclude these. I tried using pre_get_posts and then $query->set(('tag__not_in', array('53')) but I don't think tag__not_in will work with custom post types. Is there any way to fix this?
add_action( 'pre_get_posts', 'exclude_regular_tag');
function exclude_regular_tag( $query ){
if ( is_admin() || ! $query->is_main_query() )
return;
if ( is_home() ) {
return;
}
if ( is_post_type_archive( 'event' ) ) {
$args = array('53'); //id of 'regular' tag in custom post type 'event'
$query->set('tag__not_in', $args);
return;
}
}
No correct solution
OTHER TIPS
I used tax_query instead of tag__not_in and it works now.
if ( is_post_type_archive( 'event' ) ) {
$taxquery = array(
array(
'taxonomy' => 'event-tag',
'field' => 'id',
'terms' => array( 53 ), //the ID of the event tag
'operator'=> 'NOT IN'
)
);
$query->set('tax_query', $taxquery);
return;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange