As already hinted by @Milo, check if you have terms before appending your tax_query
You can try the following: (Requires PHP 5.4+ due to short array syntax, revert to old syntax if necessary)
$args = [
'post_type' => 'product',
'posts_per_page' => 15,
'paged' => $paged,
'post__not_in' => $exclude,
's' => $filter,
];
// Append our tax-query if we have terms. Make sure it is a valid string or array
$term = 'DEFINE YOUR TERM HERE';
if ( $terms ) {
$args['tax_query'] = [
[
'taxonomy' => 'product_cat'
'terms' => $terms,
]
];
}
$q = new WP_Query( $args );
Related Posts:
- Why is my WP_Query not working when tax_query terms are an array?
- WP Query for Posts (Products) in Specific Category that has 2 Specific Tags (*AND* both tags not *OR*)
- How to add terms to my tax_query based off of the current post
- Change default ordering of taxonomy terms – pre_get_terms
- Is it possible to dynamically get queried term AND taxonomy?
- Pass array of taxonomy terms to wp_query
- Get list of terms that have posts in another term
- Querying on multiple taxonomies pulled from $_GET checkbox array not working?
- How to add custom meta to ‘pre_get_terms’?
- How to print term name inside wp post loop
- Why pagination is not working with tax_query param?
- How to show terms from another taxonomy
- How can I get all the posts that are related with a specific taxonomy term?
- Custom query for tag and custom tag from 2 post type
- How to get several fields from wp_query?
- Modify WordPress Search
- When should you use WP_Query vs query_posts() vs get_posts()?
- Nested meta_query with multiple relation keys
- order by numeric value for meta value
- Wp get all the sub pages of the parent using wp query
- Multiple relationship for multiple tax_query in WP_Query
- Return only Count from a wp_query request?
- How to query for most viewed posts and show top 5
- WordPress tax_query “and” operator not functioning as desired
- WP_Query vs get_posts
- Show posts without term
- WP_Comment_Query pagination, delving into the unknown
- Usage of the new “posts_clauses” filter in WordPress 3.1?
- Conditional arguments for WP_Query and tax_query depending on if $somevar has a value
- Make a WP Query search match exactly the search term
- WP_Query: query posts by ids from array?
- When/why does ‘$query->get( ‘tax_query’ );’ return empty?
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- Escaping WP_Query tax_query when term has special character(s)
- Sort posts by category name and title
- Does tax_query really beats meta_query in all situations?
- How to uniquely identify queries?
- What is the most efficient way of querying posts based on visits and date for current day?
- Custom WP_Query order by post_meta and (author) user_meta
- Query WooCommerce orders where meta data does not exist
- Tax_query terms ID’s using variable
- WP_Query for WooCommerce Products
- pre_get_posts with get_posts
- author.php with ACF and CPTs
- Query Custom Meta Value with Increment
- When should you use wp_reset_postdata vs wp_reset_query?
- Get list of posts which have at least one term from a custom taxonomy with WP_Query
- Get posts by meta data OR title
- Identify which loop you are hooking into; primary or secondary?
- Perform query with meta_value date
- WP_Query ordered by custom field that is a date string?
- How to make an activities stream mixing posts and comments?
- Duplicate Queries
- How to query for a week using key => value WP_Query argument notation?
- Finding all results from database within 500 miles of the given latitude and longitude [closed]
- How to create a WP_Query to search the Title or Tag?
- Show only oldest post by author
- Order posts by tags count?
- if wp_query taxonomy term have posts
- SQL query equivalent to WP User Query
- Add and in the header while looping over custom query in page template
- How to get the posts published in last two days using WP_Query?
- Highlighting Sub topic in a post?
- WooCommerce: filter by parent product’s taxonomy and product variation’s meta data
- Which custom query am I in and how can I access its properties & methods?
- How to count post type that has a particular term?
- Query set using tax_query with relation ‘OR’ modifies category object?
- How to combine tax_query and date_query in WordPress
- Finding the next 5 posts
- how to retrieve specific product attribute value in an sql query?
- List taxonomy terms plus their latest post ordered by post date
- Querying by taxonomy vs Querying by Custom fields Speed Comparison
- Get random terms
- Dynamically Override Fancy Title – Part II
- How to extend tag and category “Related Posts” query to custom post_type if the first 2 terms have no posts
- Using tax_query reverses my post_type argument in a custom WP_Query
- Transient pagination not working properly
- Foreach-generated custom tax queries, each with an ajax “Load more” button
- Can not switch the queried post in pre_get_posts hook
- Find all product that contain terms with %keywords% in WP_Query
- Get term by custom term meta and taxonomy
- Order terms inside a select dropdown
- view queries made?
- meta_query on a date range using an array of values
- WP_Query Performance Issues with meta_query
- WordPress custom archive page
- How to speed up wp_query, took more 5s to run against 100k posts
- WP_query category__in not working, only pulls from first category
- How to detect custom query inside `posts_where` hook?
- tax_query shows no results if nothing is selected
- Pass the same object to multiple widgets in a template with one query
- Is temporarily overwriting $wp_query a bad idea?
- Get the post permalink within the loop but without additional DB query
- Query Set Order By Author
- WP_Tax_Query with post_tag not working
- List the 5 most recent child pages
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- Retrieve or Query Pages by ID
- get complex results set according to category structure
- Get array of current post term ID’s