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)
-
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
-
<div><input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
-
<input type="hidden" name="cat" value="22" />
-
<input type="submit" id="searchsubmit" value="Search" />
-
</div>
-
</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.


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.
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
Ps sorry for the extra characters, I meant:
<input type=”hidden” name=”cat” value=”23<
and so on so forth.
Thanks,
M.
I have the same question, how can I search multiple categories?
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!
Doesn’t work. At least not with wordpress 2.6
I really meant… 2.8.6
http://wordpressgarage.com/code-snippets/how-to-hack-the-wordpress-search-function-search-categories-and-child-categories/
This one works.
great tutorial, many thanks
i use it on my online shop
http://dressroom.xiawa.my
[...] Limitar el buscador de WordPress a ciertas categorías. Sencillamente, añadiendo un campo hidden al formulario de búsqueda. [...]
Brilliant! Thank you
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
[...] Shared Search Specific Category in WordPress. [...]