I’m trying to create a search feature that will allow users to enter a search term and select a couple of categories. The script searches a single category just fine, but when I add more than one I hit a roadblock.
My code is as follows:
<form method="get" id="searchform" action="<?php echo home_url(); ?>">
<input type="text" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Search...':this.value;" value="Search..." name="s" id="s" />
<input type="hidden" name="post_type" value="product" />
<?php wp_dropdown_categories('taxonomy=product_cat&id=make&child_of=15&show_option_all=Select Make...'); ?>
<?php wp_dropdown_categories('taxonomy=product_cat&id=model&child_of=21&show_option_all=Select Model...'); ?>
<input type="submit" id="searchsubmit" value="Search" />
</form>
As I was searching around I discovered: WordPress Multiple Category Search, which seems to do exactly what I want…however I’m not sure how to incorporate the suggestion.
I’m pretty sure you add the following to your functions file:
add_action( 'parse_request', 'category_search_logic', 11 );
function category_search_logic( $query ) {
if ( ! isset( $query->query_vars[ 'cat' ] ) )
return $query;
// split cat query on a space to get IDs separated by '+' in URL
$cats = explode( ' ', $query->query_vars[ 'cat' ] );
if ( count( $cats ) > 1 ) {
unset( $query->query_vars[ 'cat' ] );
$query->query_vars[ 'category__and' ] = $cats;
}
return $query;
}
But, how do I incorporate this into the search form? I’m sure it’s something simple, but I’m lost…anyone have any ideas?
Thanks,
Josh
I found the solution…
My functions page now looks like:
My form now looks like:
My search page now looks like: