Exclude taxonomy Query if post_type

Question

How can I define in pre_get_posts hook on custom post type to not execute the query on a taxonomy.

I have the following

if (!empty($newsType) && $newsType == 'press') {
    //if $newsType exclude the taxonomy query and get just the post_type
    $taxQuery = [
        [
            'taxonomy' => 'news_type',
            'operator' => 'NOT IN',
        ]
    ];

    $this->query->set('post_type', ['press']);
    $this->query->set( 'tax_query', $taxQuery );

} else {
    $this->query->set('post_type', ['news', 'press']);
}

If I do the following
unset($this->query->query_vars['news_type']); the query will break with

0
fefe 2 months 2022-06-09T05:10:18-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse