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="search form" action="/"> <div> <input type="text" value="" name="s" id="s" /> <input type="hidden" value="22" name="cat" id="scat" /> <input type="submit" id="search_submit" name="Search" 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://jzelazny.wpengine.com/?s=WordPress to http://jzelazny.wpengine.com/?s=WordPress&cat=22 and will only return posts in the category ID you choose.
You can query multiple categories by adding commas in between the category ids.
Would it be possible to have two pulldowns an let the visitors select one category from each, and perform a search tha woul display the results only from those two categories, please?
Category 1
Category 2
I guess…
🙂
Ups… didn’t post the code…
select name=”cat”
option value=”1″
option value=”2″
Great tip. Thank you!
Doesn’t work for me…
How can I search a category and all its subcategories (child categories) Thanks in advance!
This appears to be broken in WordPress 2.7.1.
See this:
http://wordpress.org/support/topic/266658?replies=2#post-1065708
If you take a look at that link you sent – it is not broken in 2.7.1 – there are some plugins or themes that might interfere – but it is still working and you can see it by testing out these two URLs which use the custom search strings:
Example: I am going to search my site for “XOOPS” using the normal search which includes all categories:
http://jzelazny.wpengine.com/?s=XOOPS
I get back the expected results.
Now I am going to limit my search to only category 3:
http://jzelazny.wpengine.com/?s=XOOPS&cat=3
You can see – category 3 does not contain any XOOPS posts so I get the “None Found” – just as expected because there are no posts in category 3 that have to do with “XOOPS”
Thaaaaank youuu!!
So easy and I wasted hours in my functions.php
How to get the current category ID to make the value of the hidden field dynamic?
I wanted to be able to make searches limiting the result to the category the visitor is in.
Thanks
Gabriele
PS: Again, as I said in my email, gorgeous gorgeous theme and work!
Thanks! You are a lifesaver.
i have been 3 days search for this method 🙁 yes, you save my life. thank you!
Hi!
Thanks for documenting this feature. A user of my Relevanssi plugin pointed me here, and I was able to add this same functionality to my plugin. One of those things you’d never figure out by yourself, I suppose.
If you think you could use a better search for WordPress, check out Relevanssi – it gets you better results and cool post excerpts with the search terms highlighted.
Thank you very much… This helped me and my team. Nice tutorial.
Is it possible to edit this code so it searchs a specific PAGE only?
Hello, nice tutorial. is it possibile to search only in one filed? I’d like to search only in the title field?
Thanks if you can help,
Federica
Very useful discussion. Thanks. After tinkering a bit, I created the category drop down version as an ‘advanced’ search page option. I thought about trying to use checkboxes, in place of a pull down, as my users could use that type of functionality, but will leave that for a later project. Here’s a link http://www.ewillys.com/?page_id=14814 for anyone curious about how I did this..
Thanks again,
– Dave
Would it be possible to apply this same method to a page?
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
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
I have the same. I will have multiply categories, and i want to make that users can search each category individually.
For example:
search for honda
in cars
or in motor
Anybody how could i do this?
One way to use this method and still search multiple categories is to create a master category, such as “Search Categories” and then assign each Category that you would to be searched as a child of the “Search Category”
After assigning all of the categories as children of “Search Category”, simply use the category ID for the “Search Category” category and it will search through all of the categories that were set as it’s children.
This method worked on a project that I just completed.
Thanks for the post!
Hello,
i am trying to adapt the search code so it searches only the posts of the logged in user, (or, put another way, displays search results only for the posts made by the logged in user).
I thought something involving author=%userid% might do it, however i have played around inserting it in various places (including making the url read ” /?s=KEYWORD&author%userid% ” ) but to no avail.
Does anyone know how i might do it? Is it possible simply by adapting the code like this?
Many thanks,
Adam.
Hello. I was just going to do a search post – I will include that option in the example. Do you want a checkbox or an option to search only posts by the logged in user or is that assumed?
hi. i want to have 2 options for searching.
the 1st option (which is available to everyone, logged in or not) is to search all the posts.
the 2nd option (available only to logged in users, and ideally only appearing to logged in users) is to just search their own posts. (but they need to also have the possibility of using the 1st option of searching all the posts).
i initially thought of just having a 2nd search form which appeared in the sidebar after log in (i am using the “sidebar login” plugin). but yes, i guess it could also be incorporated in to the one search form with a checkbox of some kind.
i look forward to the post!
Sounds good. I will be working on this tomorrow and will post the details!
This is now available: http://jzelazny.wpengine.com/2011/02/25/sdac-plugin-sdac-author-search/
I tried It but unfortunately It does not work for me.
okokok me has been very helpful not know how to do it with my website
thank you very much for the help it will be implemented in my site
Good one mate,
but finally I coded something which every blogger wants in his/her wp website. Its multiple category search option.
Just take a look
http://webstutorial.com/wordpress-multiple-category-search/content-management-system-cms/wordpress-cms
I love you!
very useful code,
thanks
Works!
But how to add a submit button?
You would just use your standard search input.
Hi,
Tx,
Could you tell me how?
I have added in the search button to the example above.
I See
TX!!
Thank you it worked for me..
I know this is an old post but dose this still work with the latest version and if so. How do i get to only show all category pages not just one? Thanks for taking the time to read this.
This still works