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!
Related Posts:
- Sorting a list of posts displayed under a list of associated terms (which should be sorted without initial articles)
- How to display Related Posts based on number of taxonomy terms matched
- tax_query not working in template
- WP_query – Filter by tax_query and meta_query using multiple select
- WP_Query parameter conflict
- Custom Category Walker with Image, Fallback to Most Recent Post in Category Image
- tax_query (if the terms are empty)
- custom post type and a “sticky” position taxonomy
- How to search through all child taxonomies using WP_Query?
- WP Query tax_query not returning posts
- tax_query showing no results
- WP Query Conditionally query meta and taxonomy
- $wp_query->queried_object->ID throws warning: Undefined property
- Counting Posts of a Given Post Type Having a Specific Taxonomy?
- WP_Query -> sort results by relevance (= most tags / taxonomy terms in common)
- how to group custom post type posts by custom taxonomy terms
- Displaying Posts Related to Other Posts by a Taxonomy Term?
- Querying Posts by Taxonomy From Alternate Network Site
- custom post type paging not working past page 3
- Combine tax_query and meta_query in WP_Query
- Should unaltered default loop/query return a list of custom posts belonging to a custom taxonomy?
- Multiple Archive Pages for Custom Post Types AND Taxonomies
- Query for posts in 2 taxonomies
- Exclude current post when getting related post on custom post type and taxonomy
- Display Posts of a Category in Alphabetical Order (Custom Post Type)
- Ordering Custom Post Types with WP_Query
- Paginated Taxonomy Term Archive including one post per term
- Query for custom post type objects in a taxonomy and with a meta value
- WP_Query search posts by custom post type and custom taxonomy
- Is it possible to create a shortcode that will query a post based on taxonomies?
- tax_query returning all posts instead of selective posts in WP_Query
- wp_query to find posts by year and month
- Querying Term Posts in Loop
- Custom Post Type Loop within Shortcode
- Taxonomy Archive: Display only one post per term from separate custom taxonomy
- $wpdb: Counting posts corresponding to 3 terms in 3 different taxonomies
- Query Multiple Taxominies Across Multiple Post type’s
- Display CPT posts based on specific taxonomy
- Filtering WP_Query Dynamically on the Front-End
- Why get_posts() not returning only selected category posts from Custom Post Type?
- Archive for custom taxonomy lists all posts instead of current taxonomy
- WP_Query most viewed posts, in multiple Post Types, last 30 days, excluding a specific taxonomy term
- Return one unique custom post type result when it shares a custom taxonomy in WP_QUERY?
- Custom post taxonomies as tax_query terms?
- Custom WP Query on custom meta and sort by multiple meta keys value
- How can I made custom taxonomies relationship?
- Why is this query not working? (Standard posts + custom post type)
- Custom taxonomies relationship
- Custom Widget WP_Query problem
- First custom field value (out of several) displayed twice after query
- Add Custom Post Type of specific Custom Taxonomy to regularly blog loop
- WP_Query orderby and tax_query
- Retrieve custom post types by custom taxonomies with WP_Query
- How to make WP_Query not to show irrelevant posts?
- WordPress wp_query() basic question about args
- Create an archive page for custom post type with custom taxonomy
- Custom post type, custom taxonomy, query posts only from taxonomy (children of)
- how to get this tax_query working?
- How to sort a WP_Query by a custom field AND ALSO filter by a different custom field
- How to get the post terms from a child taxonomy
- display posts of custom post type with custom taxonomy
- Custom post type and custom taxonomy 404 on page 2
- How to count other posts not having specific taxonomy terms?
- Exclude latest post from WP_Query taxonomy term loop
- Running a custom query inside another cpt single and trying to grab a variable
- Get posts by category name
- WP_Query order custom post type with certain meta key value by post modified date
- Cant’ Display Custom Post Type Title Base on Tax Terms
- Display featured posts for a custom post type by taxonomy
- Query for specific taxonomy that executes a particular loop depending on volume of posts?
- What is the most efficient way to execute recursive complex queries?
- set object terms after some some time of published post – functions.php
- Sort custom posts by date and then by taxonomy
- have to do a while have post to show for two custom post types sharing the same taxonomy?
- Display related CPT with custom taxonomy
- display post count in archive page that have relation with another taxonomy term
- WP_query sort by taxonomy
- Custom Taxonomy – fields
- CPT Loop, include taxonomies and disclude others?
- tax_query not working?
- Getting Custom Posts with Custom Taxonomy
- Not able to get my custom search result using meta_query and tax_query together?
- Error get_posts with Custom Taxonomy and OR relation
- Why get_posts() returns empty array while I am trying to get posts from some specific taxonomies and work properly with others?
- How to show post which has the specific taxonomy terms?
- Get posts of an specific term of a custom taxonomy
- Checking Taxonomy Terms for a Custom Post Type With get_the_terms Not Working
- Custom post type and custom taxonomies display
- WP_Query for CPT with filter by another WP_Query
- How to pass taxonomy terms to WP_Query along with $args?
- WordPress custom post type
- Custom Taxonomy Query by Taxonomny not working
- How to query posts by meta keys AND under specific category?
- Shortcode display CPT Query only showing 1 post?
- Storing/querying custom date data
- WP Query Post Type with same Taxonomy name
- Catergory args causing loop not to show
- Sort ACF by custom taxonomy
- Complex Custom Loop with Includes
- Dynamically count the number of custom post types associated to a custom taxonomy