0

Hello i use this code to allow my readers to filter the search results by targeting a specific category.

How can edit it to make 2 options of categories groups? I have 20 categories. I want the first option to filter 10 specific categories and the other option to filter the other 10.

<form role="search" method="get" id="searchform" action="<?php bloginfo('siteurl'); ?>">
  <div>
    <label class="screen-reader-text" for="s">Search for:</label>
    <input type="text" value="" name="s" id="s" />
    in <?php wp_dropdown_categories( 'show_option_all=All Categories' ); ?>
    <input type="submit" id="searchsubmit" value="Search" />
  </div>
</form>

Thank you.

1 Answer 1

1

You can use in search query wp public query vars.

When searching in multiple categories, wordpress accepts comma separated category ids in url &cat=1,2,5

Now I can come up with only such simple solution.

  1. Get rid of wp_dropdown_categories
  2. use e.g. get_categories, so you can customize SELECT attribute name into cat[] (which is also possible with wp_dropdown_ca..., but keep with me)
  3. set SELECT attribute to multiple
  4. after submitting form when user has choosen multiple values, you should get an POST array of category IDs, you now format them into comma separated values and redirect the user into the new URL, e.g. www.site.com/?s=cars&cat=1,45,45
  5. The idea about redirecting the user to URL, which WordPress can read and provide you a proper result

Another way is, when an user is picking categories, with Javascript you get the picked options and set a hidden input like so <input type="hidden" name="cat" value="1,5,10">. Now you do not have a need to additional redirect.

(My sentiment was only to provide a way, how it can be done. Take my solution only as illustration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.