To achieve this you need to conditionally add the clauses, right now you always have all 3, even if only 2 are selected.
So instead pass in a variable:
$tax_queries = [
'relation' => 'AND',
];
Notice I used the modern []
syntax for arrays instead of array()
, and I added the relation
field as we always want that.
Then, add each clause by testing those variables, e.g. for the book category:
if ( ! empty( $_GET['book-category'] ) ) {
$tax_queries[] = [
'taxonomy' => 'book_category',
'field' => 'slug',
'terms' => $_GET['book-category'],
];
}
This would also be a good time to do any trimming or processing to remove trailing spaces etc.
Do this again for the other 2 clauses/fields. Then finally, use the variable in the query:
'tax_query'=> $tax_queries,
Related Posts:
- How to have this permalink structure: post_type/postname/custom_inner_page
- Advanced search form with filters for custom taxonomies
- Searching post types
- How to search through all child taxonomies using WP_Query?
- Live search by custom tag
- Why get_posts() returns empty array while I am trying to get posts from some specific taxonomies and work properly with others?
- Include custom taxonomy term in search
- Custom post type single-{custom}.php not working
- AJAX search on post pages by custom post type
- Exclude a category from WP_Query
- Select All in Parent Category, Group by Child Category?
- How to build a complex page structure
- get_query_var() not working in pre_get_posts
- query multiple taxonomies
- How to replicate some of Drupal Views functionality in WordPress?
- How to get_queried_object on multiple objects?
- WordPress Search documentation: how to improve search query using taxonomy terms, custom meta fields?
- Custom search for custom post type, custom meta and search fields
- Sorting a list of posts displayed under a list of associated terms (which should be sorted without initial articles)
- Sort the main query in subcategories/terms?
- Custom Taxonomy List links being re-written
- Want to filter only parent post in admin area
- Custom Taxonomy and tax_query Issue?
- Custom post query by taxonomy
- Separate Posts and Custom Post Type in Custom Taxonomy archive template
- Show Post Count of a Category
- Group custom posts by custom taxonomy names
- How can i skip same post from taxonomy term?
- Query/list all terms and their custom post count
- Wrong request query on cpt and tax
- Search Custom Post Type & Taxonomies
- Get custom taxonomies from multiple posts
- Pagination for Custom Taxonomy Page [duplicate]
- Search / Filter posts on Title/Content OR Tags
- Custom loop with multiple taxonomy queries
- Help need making decision. Ads rotating site on wordpress
- Custom taxonomy rewrite give pagination 404
- WP_Query inside foreach loop returning same value for all options when filtered using ajax
- What’s the WP way to load remaining custom posts?
- Get unique superset of taxonomy terms from a list of custom posts in another hierarchical taxonomy
- Auto Complete Search
- Search by tag name and category
- Include custom taxonomy term in search
- Why is my WP_Query outputting my entries twice?
- WP the_posts() on single-cars.php get category link
- tax_query not working in template
- Custom Taxonomy Not Showing in Front-End When Outputting a Custom Post Type with WP_Query()
- Custom WP_Query doesn’t display all posts
- Creating two loops based on different logic
- WP_Query parameter conflict
- Custom Taxonomy Search result page
- Show Taxonomies with admin area for custom post type?
- Include custom post type custom taxonomies in Categories widget
- Hide custom post type from search based on custom taxonomy
- tax_query (if the terms are empty)
- How to count other posts not having specific taxonomy terms?
- List all Custom Post Type posts excluding certain Taxnomy term
- Get posts by category name
- Multi Parameter search with custom post type category and tag
- Query custom posts from custom taxonomy
- Query Custom Post Type by Taxonomy
- Multiple custom post type queries causing wrong post types to be grabbed in taxonomy + single templates?
- How can I tell if I’m on a custom post type archive page?
- Query for specific taxonomy that executes a particular loop depending on volume of posts?
- Related “custom post type” using “custom taxonomy”
- How to Implement Search Functionality?
- How to have multiple search result pages in wordpress with taxonomy terms listed
- custom post type and a “sticky” position taxonomy
- Custom post type 404 category page
- Display post count for a specific month
- WP_Query to display number of custom post type filtered (order by) taxonomy
- post_type incorrect for custom post type
- WP_Query based on ‘s’ and tax_queries
- Ajax load more inside custom post type taxonomy
- WP Query tax_query not returning posts
- Create a custom wp_query from a specific custom post type with specific taxonomy
- The most recent post of custom taxonomy
- Problems with the WP_Query query for a Custom post type
- Display all Custom taxonomy terms and their relevant custom posts
- Query pulling all taxonomies, not one I am specifying
- CPT Efficient way to display posts from different categories with custom query
- Custom post type with custom taxonomy permalinks
- Get archive of posts separated by taxonomies and subtaxonomies
- Query Pulling the same post twice
- Custom post type and custom taxonomies display
- WP_Query with custom post type search showing all results every time
- Extensive search filtering and results->PDF in the front-end
- Post data in separate divs with incrementing class using WP_Query
- Values inside a custom field to determine which category posts to display
- WP Query WordPress Post Type with the same Taxonomy name
- How to Create Custom Post Type with Multiple Color Options?
- How to list groupings and keep pagination
- How to retrieve taxonomy terms of the current custom post?
- Advanced search form with filters for custom taxonomies and custom fields
- Listing all custom post types using a specific term on the said term’s template page, in groups
- How to Get The Taxonomy Term in Custom Post Type Loop Inside a Wp Query
- display custom post type from register taxonomy
- Searching Custom Posts content with JQuery
- WordPress search results grouped by post type
- WP Query Conditionally query meta and taxonomy