Search Specific Category in WordPress

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.

64 thoughts on “Search Specific Category in WordPress”
  1. mc2w |

    You can query multiple categories by adding commas in between the category ids.

  2. Ivan | SEO Consultant.ie |

    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?

  3. viktor |

    Category 1
    Category 2

    I guess…

    🙂

  4. viktor |

    Ups… didn’t post the code…

    select name=”cat”
    option value=”1″
    option value=”2″

  5. Adriana |

    Great tip. Thank you!

  6. Levani |

    Doesn’t work for me…

  7. Rebecca |

    How can I search a category and all its subcategories (child categories) Thanks in advance!

  8. Paul |

    This appears to be broken in WordPress 2.7.1.

    See this:
    http://wordpress.org/support/topic/266658?replies=2#post-1065708

    • jzelazny |

      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”

  9. xzombi |

    Thaaaaank youuu!!
    So easy and I wasted hours in my functions.php

  10. Gabriele |

    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!

  11. Tony |

    Thanks! You are a lifesaver.

  12. brokencode |

    i have been 3 days search for this method 🙁 yes, you save my life. thank you!

  13. Mikko |

    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.

  14. Coolzrock |

    Thank you very much… This helped me and my team. Nice tutorial.

  15. ITMDesign |

    Is it possible to edit this code so it searchs a specific PAGE only?

  16. Federica |

    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

  17. David Eilers |

    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

  18. Travis W |

    Would it be possible to apply this same method to a page?

  19. Alan |

    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.

  20. Michel |

    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

  21. Michel |

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

    and so on so forth.
    Thanks,
    M.

  22. Laveni |

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

  23. Chris M |

    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!

  24. Alek |

    Doesn’t work. At least not with wordpress 2.6

  25. Xiawa |

    great tutorial, many thanks 🙂

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

  26. Michael Bailey |

    Brilliant! Thank you 🙂

  27. ben |

    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

    • ipke |

      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?

  28. Wes D |

    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!

  29. adam |

    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.

    • jzelazny |

      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?

      • adam |

        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!

  30. awidea |

    I tried It but unfortunately It does not work for me.

  31. jonh | descargar-ares |

    okokok me has been very helpful not know how to do it with my website

  32. anton | djrajobos |

    thank you very much for the help it will be implemented in my site

  33. Niraj |

    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

  34. michael |

    I love you!

  35. rams |

    very useful code,
    thanks

  36. Wop |

    Works!

    But how to add a submit button?

  37. Wop |

    I See

    TX!!

  38. Manjesh V |

    Thank you it worked for me..

  39. Hero |

    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.

Leave a Reply

Your email address will not be published.
*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.