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
- Archive for custom taxonomy lists all posts instead of current taxonomy
- Genesis filterable portfolio isotope [closed]
- wp_query check if integer exists in custom field’s array
- Wrong request query on cpt and tax
- Get custom taxonomies from multiple posts
- Pagination for Custom Taxonomy Page [duplicate]
- Search / Filter posts on Title/Content OR Tags
- WP_Query most viewed posts, in multiple Post Types, last 30 days, excluding a specific taxonomy term
- Custom-Posttype & Custom Taxonomy WP_Query
- Custom loop with multiple taxonomy queries
- Return one unique custom post type result when it shares a custom taxonomy in WP_QUERY?
- Tax query not producing any results
- How to Filter custom post type by taxonomy?
- Custom taxonomy rewrite give pagination 404
- WP_Query inside foreach loop returning same value for all options when filtered using ajax
- Custom post taxonomies as tax_query terms?
- Custom WP Query on custom meta and sort by multiple meta keys value
- WordPress loop: Show only a Custom Post Type Taxononmy TERM
- How can I made custom taxonomies relationship?
- What’s the WP way to load remaining custom posts?
- Why is this query not working? (Standard posts + custom post type)
- Custom taxonomies relationship
- Custom Widget WP_Query problem
- How do I display the grand child items of a taxonomy term?
- How to have this permalink structure: post_type/postname/custom_inner_page
- Query Custom Post Type Taxonomy term with multiple parameters
- 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
- Advanced search form with filters for custom taxonomies
- Why is my WP_Query outputting my entries twice?
- Retrieve custom post types by custom taxonomies with WP_Query
- WP the_posts() on single-cars.php get category link
- Custom Taxonomy Not Showing in Front-End When Outputting a Custom Post Type 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
- Using page slug in wp_query
- WP_Query() with custom post type and taxonomy — get all terms?
- Custom taxonomy wp_query woes.
- Variable not working in WP_Query
- Creating two loops based on different logic
- Wp Query with multiple custom tag(taxonomy) by get the terms
- Custom post type, custom taxonomy, query posts only from taxonomy (children of)
- how to get this tax_query working?
- Different options per post type in WP_Query
- How to sort a WP_Query by a custom field AND ALSO filter by a different custom field
- How to show all taxonomies within custom post type loop
- 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
- How to query Posts from a custom post type which contains a custom taxonomy?
- Query Posts that have Custom Taxonomy
- List all Custom Post Type posts excluding certain Taxnomy term
- Running a custom query inside another cpt single and trying to grab a variable
- Get posts by category name
- Foreach loop returning more than one item when querying taxonomy
- 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
- Query custom posts from custom taxonomy
- Loop for custom-post-type comparing taxonomy terms for “related” posts?
- Query Custom Post Type by Taxonomy
- Display featured posts for a custom post type by taxonomy
- Multiple custom post type queries causing wrong post types to be grabbed in taxonomy + single templates?
- Query for specific taxonomy that executes a particular loop depending on volume of posts?
- Related “custom post type” using “custom taxonomy”
- What is the most efficient way to execute recursive complex queries?
- Sorting the Loop by Taxonomy Value
- “Menu order” being ignored when querying posts from a parent taxonomy with “include_children” set to true
- Custom Query: Multiple CPTs and a taxonomy filter
- custom taxonomy pagination 404 error
- Ordering by meta value not working
- 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 for a specific month
- display post count in archive page that have relation with another taxonomy term
- WP_Query to display number of custom post type filtered (order by) taxonomy
- WP_Query for custom taxonomies showing posts from non-specified terms?
- Live search by custom tag
- Query a custom taxonomy in a function to create an csv file
- WP_Query based on ‘s’ and tax_queries
- Custom search for a custom post type in WordPress
- WP_query sort by taxonomy
- Custom Taxonomy – fields