The Operator “NOT IN” Does Not Work In tax_query

I suspect you need an array for the terms – although I’m not sure why it would work with “IN” and not with “NOT IN”… But I’d try this:

function menta_pre_get_posts( $query ) {
if ( !is_admin() && $query->is_search() && $query->is_main_query() ) {
    $term = get_term_by('slug', get_query_var('s'), 'product_tag');
    if ( $term && !is_wp_error( $term ) ) {
        $tax_query = array(
                'taxonomy'  => 'product_tag',
                'field'     => 'slug',
                'terms'     => array($term->slug),
                'operator'  => 'NOT IN'
        );
        $query->tax_query->queries[] = $tax_query;
        $query->query_vars['tax_query'] = $query->tax_query->queries;
        $query->set('tax_query', $query->tax_query->queries);
     }
}}
add_action( 'pre_get_posts', 'menta_pre_get_posts', 1 );

Hope this helps!

Leave a Comment