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
- How can I use wp_query to show all product data using just the products ID?
- Combine two filters into a single call
- Remove a filter added by a plugin
- Change the site tagline (or similar) based on current page
- Get Current User Id Inside a Loop Returns 0 For a Shortcode
- Random order of WP_Query results with highest meta value
- Handling Body class based on Template
- Custom page archive query by url var calling page twice on one of my custom queries?
- WordPress custom slug (endpoint) and compare all links
- Get_avatar filter?
- WooCommerce Related Products by Attribute
- Custom filter in admin edit custom post type responding with invalid post type?
- How to query for posts with a null or blank post_name?
- WordPress pagination not working with search page
- get current custom post ID by WP_Query method
- Add a class to the anchor tag on HTML5 gallery
- Nesting if statements to echo only one string based on what tag was filtered?
- How to properly loop through these external URLs to get them into the sitemap using this hook
- Input type search in Menu
- How to securely provide a $_POST var in WP_Query with PHP 7?
- Using foreach loop breaks
- Create WP_Query to search for posts by their categories or their parent/child categories
- WP Query with custom Shortcode
- add_action shortcut?
- wp_query with cat not working
- Advanced Meta Query for Large Calendar Website (12k+ posts) (175k+ wp_postmeta rows)
- Is it acceptable to treat a filter like an action?
- How to use an associative array in post__in with WP_Query?
- If Query In Sidebar
- Recent Posts slider, using WP_Query(), loads duplicate sliders
- Compare post-IDs within WP_Query? (Less than / Greater than)
- How can I remove a function that has been added to wordpress with add_filter?
- How to control WordPress image metadata (using Imagick)?
- Edit incorrect password message WordPress
- How do I add custom bulk actions to multiple custom post types?
- How to use $query->set with post__in?
- Add data attribute to each li in menu
- Show prices with tax in Woocommerce Mini Cart [closed]
- Why do WP_Query results change after updating unrelated Advanced Custom Fields (ACF)?
- Ajax category filtering products default show all
- Add a specific part of current category page url to shortcode
- Why does my callback function not work with this custom filter hook?
- Loading the same WP_Query in two different wordpress .php templates
- How to prevent WP_Query function from returning all posts when empty?
- Dynamic content based on a URL parameter
- Display a specific category of products in shop page and disable code for specific actions
- Contact Fom 7 – how to add custom HTML inside span.wpcf7-form-control-wrap AND IMMEDIATELY AFTER input.wpcf7-form-control?
- Removing “wpautop” (auto tags) only on certain pages?
- WP_Query adds “(wp_posts.ID = ‘0’)” so no results are returned
- creat filter with wp_query
- Add filter when image is uploaded?
- Remove the first 5 characters of the_title and orderby that
- Remove a div from RSS feed
- Print last modified date only on posts
- Add more button if more 8 items
- How to store post ID’s in cookie or session to display the same posts later
- How to add an arrow to menu items has submenus
- WordPress Gravatar filter is removing my custom attributes
- Generating an nonce for Content Security Policy and all scripts – How to make it match/persist for each page load?
- WordPress, AJAX and pre_get_posts using conditional tags
- Get users that likes the post
- Unique icons next to each WordPress menu item
- get_posts() and WP_query limits ‘AND’ conditions to a maximum of 6 for meta value queries in WordPress
- Exclude posts based on meta value
- Recent Posts Not Showing Only On A Specific Category Page [closed]
- Custom query, checking values of multiple meta keys
- auto-populating custom nav with all items from custom post type
- inserting a post from an extern php file but post content doesn’t show on wp site
- Change tinyMCE editor to allow marked content when pasting from Word
- Remove echo from shortcode
- Problem with login form
- 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
- WordPress theme options error
- Redirect to another page using contact form 7? [closed]
- Security for data obtained from the database
- Alter required message using comment form api
- Fatal error: Call to a member function query() on a non-object
- How to debug this search & replace strings snippet?
- Hide Featured Image Meta Box on Editor Screen
- Add item to top of menu using a filter in functions.php
- Get posts from multiple post type
- Is it possible to retrieve all posts with a certain value for metadata?
- How to get all author posts outside of author templates
- WP_Query fails despite having 1 post
- Add filter multiple times using only one master function
- How to edit php code in WordPress Post file?
- Create a Blog Template Page
- Change page title from page using php via php executed from page/post itself
- How to make modifications only to certain elements of an HTML string on the server-side?
- Override a function defined in wp-includes/comment-template.php
- unable to use ‘new WP_Query’ in AJAX call
- wp_query get the 2nd post
- Exclude posts with specific metadata from search?
- How to get post ID in a Page?
- Display an image if odd number of posts in grid