Search Specific Category in WordPress

There are some times when you want to limit your search to a particular category, or perhaps multiple categories. This is relatively simple to do in WordPress by adding a hidden field to your search code. (See example below)

PHP:
  1. <form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
  2. <div><input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
  3. <input type="hidden" name="cat" value="22" />
  4. <input type="submit" id="searchsubmit" value="Search" />
  5. </div>
  6. </form>

You can see I added my hidden input field on the third line. When I add this in, it then adds onto the query used to search. Your search will go from something like http://www.sandboxdev.com/?s=WordPress to http://www.sandboxdev.com/?s=WordPress&cat=22 and will only return posts in the category ID you choose.

34 Responses to “Search Specific Category in WordPress”

  1. Alan says:

    Thank you for this, it was a great help to me.

    As Gabriele above asked -if the cat id could be dynamic it would be great too.

  2. Michel says:

    Hi,

    I was wondering to know if it is possible to search multiple categories?

    I tried <?input type=”hidden” name=”cat” value=”22,23,24″ /<? but that didn’t seem to work. I also tried to duplicated that line and assign a different category:
    <?input type=”hidden” name=”cat” value=”22<?
    <?input type=”hidden” name=”cat” value=”23<?
    <?input type=”hidden” name=”cat” value=”24<?

    But that didn’t work too. I would really appreciate any feedback.
    Many thanks and Great Post!
    Michel

  3. Michel says:

    Ps sorry for the extra characters, I meant:
    <input type=”hidden” name=”cat” value=”23<

    and so on so forth.
    Thanks,
    M.

  4. Laveni says:

    I have the same question, how can I search multiple categories?

  5. Chris M says:

    I can see searching for multiple categories as a lot more complicated than a single category, as you would need to pass an array and then strip it and use a query_posts() and not just the usual while have_posts loop unfortunately.

    Sorry to bring bad news folks!

  6. Alek says:

    Doesn’t work. At least not with wordpress 2.6

  7. Xiawa says:

    great tutorial, many thanks :)

    i use it on my online shop
    http://dressroom.xiawa.my

  8. [...] Limitar el buscador de WordPress a ciertas categorías. Sencillamente, añadiendo un campo hidden al formulario de búsqueda. [...]

  9. Brilliant! Thank you :)

  10. ben says:

    hi,
    I’d appreciate a little help..

    i am trying to create a search within my wordpress site that will combine 2 choices (made by the user). Both choices are drop downs, the first is the categories list (could also be hard coded, as it will not change), and the second is a list of KWs that are present as tags in the relevant posts (again, can be hard coded too).

    the user technically searches for a specific tag within a specific category (but needs to look as if he/she are choosing option A and option B, as search criterias…

    hope that made sense,
    please help…

    thanks

    Ben

  11. [...] Shared Search Specific Category in WordPress. [...]

Leave a Reply