You could build an intermediary array for the tax_query
parameter:
$tax_query = array();
$taxonomies = array(
'product_cat',
'sub_category',
);
foreach ( $taxonomies as $taxonomy ) {
if ( ! empty( $_POST[ $taxonomy ] ) ) {
$tax_query[] = array(
'taxonomy' => $taxonomy,
'terms' => $_POST[ $taxonomy ],
);
}
}
query_posts(
array(
// …
'tax_query' => $tax_query,
),
);
Related Posts:
- Custom query vars filters problem with pagination
- How can I modify this code to make the search box include tags and meta
- How do I make these combination select filters work when only one dropdown is set? They work in WordPress 5.8.2 but don’t in 5.8.3
- Keeping the previous get value and add another value when submitted
- Show post in elementor based on views and specific category with query id feature in elementor
- Set media upload attachment link to none and hide it in WP v3.5
- How to set custom cookies before output
- Build a content and excerpt grid loop with paging and options for # of posts
- How can I hook into and edit the text of a wp_nav_menu tag?
- Rename image uploads replacing “width” and “height” in filename with “medium”, “large”, “small”
- How to make an meta_query optional?
- Check if page parent has certain template
- How to remove “out of stock” variation options from size dropdown in woocommerce?
- wp_delete_post deletes all posts instead of just expired ones
- Trim first 2 words of the exceprt
- wp_get_attachment_url filter won’t accept two arguments
- Filter link to existing content suggestion
- Getting my head round WordPress filter
- Display Post co-authored in Author Page
- How to set a custom path, for ajax image upload folder in admin?
- Sorting Posts | Alphabetical Order
- How to use return in my custom function instead of echo
- Looking for most performant way to execute several similar WP queries within shortcodes
- When using the_author hook, how can I determine the PHP file that generates each call to `the_author()`?
- WordPress php filter admin_body_class not working
- Get the last month with posts using a recursive function…
- How to query for pages/post depending on slug?
- Combine two filters into a single call
- Handling Body class based on Template
- Add a class to the anchor tag on HTML5 gallery
- How to use an associative array in post__in with WP_Query?
- Show prices with tax in Woocommerce Mini Cart [closed]
- Display a specific category of products in shop page and disable code for specific actions
- Removing “wpautop” (auto tags) only on certain pages?
- WordPress Gravatar filter is removing my custom attributes
- WordPress, AJAX and pre_get_posts using conditional tags
- Exclude posts based on meta value
- Custom query, checking values of multiple meta keys
- Remove echo from shortcode
- How Can I use WP_Query to Only Display 1 Post from Custom Post Type if Query Returns Posts with Matching ID in Custom Field
- Fatal error: Call to a member function query() on a non-object
- Is it possible to retrieve all posts with a certain value for metadata?
- unable to use ‘new WP_Query’ in AJAX call
- Filtering a function’ output for a new continued function
- Deleting Certain terms from appearing on the front end as links
- Trouble inputting variable into WP query
- Add meta tags to a custom header
- Ajax filter button display all posts
- Array to string conversion on array_map
- Target post_date_gmt if it’s empty or not set
- How to hide posts of a specific custom category in WordPress?
- WordPress meta_query >= &
- WordPress WP_Query without query GET parameters
- Get the name of menu item with wp_nav_menu
- update_post_meta() not saving data inside of save-post filter
- Dividing the loop to style post differently
- How to add aria role and schema markup to custom walker container
- Must filter functions receive all arguments passed to them?
- displaying a fall back query if there’s nothing in the post-type category
- How to internationalize header image?
- User filter posts by year
- Output product category link from WP_Query
- Isn’t Returning Value While Using SELECT COUNT(*) FROM {$wpdb->prefix}
- wp_query with multiple arguments with AND
- Nested Queries of decreasing specificity
- Wrong pagination results, page counter resets when navigating
- Variation prices breakdown only for single product page
- Is it possible to order posts using multiple meta queries, i.e. show posts from first meta query, then the second?
- in_array not working on dev server but works on localhost
- Custom search query on WordPress page not working
- WP query posts BUT show the most recent and one per author
- Define global custom post
- How to use apply_filters() inside a plugin class?
- Multiple Tag Filtering
- Need to show 7 posts from actual date
- Search for single post by 2 tags
- Get Child of Child Pages in custom Menu
- How can i style “echo apply_filters”
- Meta query not showing result properly
- Woocommerce Related Product by 2 taxonomy
- Add class to all meta boxes for a custom post type
- making a search.php query
- Efficient way of querying for a “fallback” post?
- Can’t get data from audio media
- Create a hierarchical loop at predefined markup requirements
- WP_Query count of different meta key values [duplicate]
- Select All not working in a WordPress search filter
- Query seems to be duplicated
- WXR XML import is stripping php tags that I need to keep
- AJAX: WordPress filters inside $html do not work as intended
- Fix incorrect related posts code snippet
- Why not showing all post by default in my jquery filter
- wp_query on search results page showing all results every time
- Assign new post author IF another user in custom field on post transition
- Blog Posts not sorted and pagination not adjusted after filtering some category
- How to change wp-admin and wp-login urls
- How to use a conditional statement in a post loop but not count towards the “posts_per_page” if false
- Is there a hook that I can use when a fatal error occurs?
- wp_logout action hook is not firing
- How to make posts_like_dislike reference current post when iterating?