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?
- Is it possible to create a shortcode that will query a post based on taxonomies?
- Keyword search limited to specific post type filtered by multiple custom taxonomies?
- need advice on how to do a lists using custom post types – taxonomy vs postmeta
- wp_query to find posts by year and month
- How to create an advanced filter search?
- Querying Term Posts in Loop
- Custom Post Type Loop within Shortcode
- Fetch taxonomies by custom post type id array
- Group custom posts by custom taxonomy names
- How to Get Current Custom Post Type Selected Taxonomy Term (Not All Terms)
- How can i skip same post from taxonomy term?
- Filter by custom taxonomy slug on a custom post type
- Displaying a custom post types custom taxonomy value?
- Taxonomy Archive: Display only one post per term from separate custom taxonomy
- Including Custom Meta with posts_where query
- Pagination : How to remove /page/x/ after a ‘POST’ action on a form returning to page 1
- Display CPT posts based on specific taxonomy
- shortcode to show custom post types by category
- Query/list all terms and their custom post count
- Filtering WP_Query Dynamically on the Front-End
- What template files do I need to customise custom-category-term-links rather than fall back on archive.php?
- Why get_posts() not returning only selected category posts from Custom Post Type?
- 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
- 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
- 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?
- search also in taxonomy, tags and custom fields
- Help need making decision. Ads rotating site on wordpress
- custom taxonamy and post type
- WordPress sort search results by custom order
- Return the latest post from a custom Taxonomy and Post Type
- Don’t know how to show custom taxonomies from a custom post_type
- Custom taxonomy rewrite give pagination 404
- WP_Query inside foreach loop returning same value for all options when filtered using ajax
- Custom WP Query on custom meta and sort by multiple meta keys value
- Display a grid of taxonomy terms at root taxonomy page
- Including metaboxes from custom post types in global search — continued
- 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?
- Get unique superset of taxonomy terms from a list of custom posts in another hierarchical taxonomy
- Custom taxonomies relationship
- Pretty URL for custom search for custom post type
- Query Custom Post Type Taxonomy term with multiple parameters
- Auto Complete Search
- 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
- Search by tag name and category
- How to produce a sub-page-system in WordPress
- dropdowboxes with pre-selected custom fields optios that filters the results according seach needs
- Include custom taxonomy term in search
- Why is my WP_Query outputting my entries twice?
- How to display Related Posts based on number of taxonomy terms matched
- Retrieve custom post types by custom taxonomies with WP_Query
- 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()
- How to make WP_Query not to show irrelevant posts?
- WordPress wp_query() basic question about args
- Search Function not searching correct custom_post_type even though post_type is defined in search args
- Custom Post Taxonomy Template Not Loading Properly
- WP_Query to display number of custom post type filtered (order by) taxonomy
- WP_Query for custom taxonomies showing posts from non-specified terms?
- post_type incorrect for custom post type
- How to display only child category post in related posts in custom post type?
- 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
- CPT Loop, include taxonomies and disclude others?
- 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
- Create Second Search Page Only for Custom Post Type
- Problems with the WP_Query query for a Custom post type
- Displaying WordPress posts from post and custom post type in custom taxonomy
- Display all Custom taxonomy terms and their relevant custom posts
- Help with Multi Level Category Archive Page
- Get posts of an specific term of a custom taxonomy
- Query pulling all taxonomies, not one I am specifying
- Conditional posts in WP_query for search
- Checking Taxonomy Terms for a Custom Post Type With get_the_terms Not Working
- CPT Efficient way to display posts from different categories with custom query