Search Specific Category in WordPress

53

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" />
</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.

53 Responses to “Search Specific Category in WordPress”

  1. mc2w says:

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

  2. 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 says:

    Category 1
    Category 2

    I guess…
    :)

  4. viktor says:

    Ups… didn’t post the code…

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

  5. Adriana says:

    Great tip. Thank you!

  6. Levani says:

    Doesn’t work for me…

  7. Rebecca says:

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

  8. Paul says:

    This appears to be broken in WordPress 2.7.1.

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

    • jzelazny says:

      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://www.sandboxdev.com/?s=XOOPS

      I get back the expected results.

      Now I am going to limit my search to only category 3:
      http://www.sandboxdev.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 says:

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

  10. Gabriele says:

    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 says:

    Thanks! You are a lifesaver.

  12. brokencode says:

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

  13. Mikko says:

    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 says:

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

  15. [...] we want to search one category, we could have used this solution which lets you manually add category numbers to the search [...]

  16. ITMDesign says:

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

  17. [...] we want to search one category, we could have used this solution which lets you manually add category numbers to the search [...]

  18. Federica says:

    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

  19. 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

  20. Travis W says:

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

  21. 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.

  22. 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

  23. Michel says:

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

    and so on so forth.
    Thanks,
    M.

  24. Laveni says:

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

  25. 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!

  26. Alek says:

    Doesn’t work. At least not with wordpress 2.6

  27. Xiawa says:

    great tutorial, many thanks :)

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

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

  29. 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

    • ipke says:

      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?

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

  31. Wes D says:

    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!

  32. [...] Search Specific Category in WordPress $(document).ready(function(){ lastBlock = $("#a1"); maxWidth = 350; minWidth = 75; [...]

  33. adam says:

    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 says:

      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 says:

        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!

  34. awidea says:

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

  35. [...] SEARCH SPECIFIC CATEGORY IN WORDPRESS Search By Category WordPress Plugin WordPress Searching By Category Multiple Category ‘Super’ Search Box [...]

  36. [...] back – I wrote a post about searching in certain categories with WordPress. One of the comments asked for a way to do this by author instead of by category. I promised a followup and I finally [...]

  37. [...] SEARCH SPECIFIC CATEGORY IN WORDPRESS Search By Category WordPress Plugin WordPress Searching By Category Multiple Category ‘Super’ Search Box [...]

  38. [...] SEARCH SPECIFIC CATEGORY IN WORDPRESS Search By Category WordPress Plugin WordPress Searching By Category Multiple Category ‘Super’ Search Box [...]

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

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

  41. Niraj says:

    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

  42. michael says:

    I love you!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>