I have a search form with one standard text input and a select input to select a taxonomy term.
The query uses the inputs to run a WP_Query and display the results. The only issue is that the query doesn't seem to run the taxonomy parameter. I can enter a keyword in fine and it will display the correct results however it doesn't pickup the taxonomy parameter. If I don't enter any keyword but select a taxonomy term from the select input then no results are found.
The value from the select input is being passed through, it's in the URL on the search results page and I've echoed it onto the results page.
When I echo the WP_Query request it doesn't detail any attempt to lookup products in the custom taxonomy 'product-category'.
Here's the form -
<form method="get" id="advanced-searchform" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="text" name="product-name" value="" placeholder="Enter product name or code">
<input type="submit" value="Search">
<select name="category">
<option value="" disabled selected>Select category</option>
<?php
$post_type = 'product';
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type, 'order' => 'ASC', 'orderby' => 'date' ) );
foreach( $taxonomies as $taxonomy ) :
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
?>
<option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
<?php
endforeach;
endforeach;
?>
</select>
<input class="hidden-search" type="hidden" name="search" value="advanced">
</form>
Here's the Query -
$_keyword = $_GET['product-name'];
$_cat = $_GET['category'];
$product_args = array(
's' => $_keyword,
'post_type' => 'product',
'posts_per_page' => 10,
'taxonomy' => 'product-category',
'terms' => $_cat
);
$productSearchQuery = new WP_Query( $product_args );
echo $productSearchQuery->request;
Have also tried -
$product_args = array(
's' => $_keyword,
'post_type' => 'product',
'posts_per_page' => 10,
'tax_query' => array( array(
'taxonomy' => 'product-category',
'field' => 'slug',
'terms' => $_cat,
) ),
);
Query Request example -
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1=1
AND ( 0 = 1 )
AND wp_posts.post_type = 'product'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private')
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 10