0

I am trying to implement a product category search. Here is my searchform.php Here is the image of what I intent to achieve enter image description here

<form id="searchform" method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <input type="text" class="search-field" name="s" placeholder="Search Product" value="<?php echo get_search_query(); ?>">
    <input type="hidden" name="post_type" value="product" />
    <select name="product_cat">
        <option value='0'> All </option>
        <option value='weitere-losungen'>weitere-losungen</option>
    </select>
    <input type="submit" value="Search">
</form>

?s=&post_type=product&product_cat=0

This as a search parameter returns all products. But when I select any other category ?s=&post_type=product&product_cat=weitere-losungen it returns zero products.

4
  • 2
    are you sure it's the slug you want and not the term ID? Are you using a secondary post loop to display the results ( WP_Query get_posts query_posts etc )? Or are you using the main WP query that WP creates
    – Tom J Nowell
    Aug 11 at 10:58
  • I'm using the normal WP_Query. What I want is the slug not term_id Aug 11 at 13:15
  • ah then that explains everything, the form you're using will modify the main query, but you've ignored the main query, and created a second brand new query in your template, so none of the forms changes will take effect. WordPress is indeed taking your form into account, but since there is no main loop the results are never shown. If you want to modify the main query, you should use the pre_get_posts filter to modify it, don't create brand new additional queries using new WP_Query.
    – Tom J Nowell
    Aug 11 at 13:39
  • And to re-iterate, I didn't mean are you sure you want to see a slug in the URL, I meant are you sure that the slug is what WP needs and not the term ID? But it sounds like wether it works or not, you've chosen to ignore the main query anyway.
    – Tom J Nowell
    Aug 11 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.