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

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 to and will only return posts in the category ID you choose.

About the Author...

Jennifer Zelazny

I am a Penn State alumni, avid golfer, kick boxer, volunteer, and Chicagoland WordPress developer who always enjoys a refreshing Coca-Cola.

  • 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″

  • Adriana

    Great tip. Thank you!

  • Doesn’t work for me…

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

  • Paul

    This appears to be broken in WordPress 2.7.1.

    See this:

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

      I get back the expected results.

      Now I am going to limit my search to only category 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.


    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.

  • Pingback: How to Hack the Wordpress Search Function: Search Categories and Child Categories | Wordpress Garage()

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

  • Pingback: Disha Hosting » How to Hack the Wordpress Search Function: Search Categories and Child Categories()

  • 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,

  • 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 for anyone curious about how I did this..

    Thanks again,

    – Dave

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

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

  • Michel


    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.

  • Laveni

    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!

  • Alek

    Doesn’t work. At least not with wordpress 2.6

    • Alek

      I really meant… 2.8.6

  • Alek
  • great tutorial, many thanks 🙂

    i use it on my online shop

  • Pingback: » Blog Archive » Limitar el buscador de WordPress a ciertas categorĂ­as()

  • Brilliant! Thank you 🙂

  • ben

    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…



    • 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?

  • Pingback: Doug Hogan » weekly life()

  • 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!

  • Pingback: WordPress?????????? | Wordpress???()

  • Pingback: 8 useful receipes to improve your WordPress theme()

  • 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,


    • 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!

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

  • Pingback: Ziyaretçilerinizi sitenizde tutacak eklentiler ve teknikler @ ?nternet Magazin()

  • Pingback: SDAC Plugin: SDAC Author Search | Sandbox Development and Consulting Inc.()

  • Pingback: Busca em categoria especĂ­fica | Bruno Monteiro()

  • Pingback: Ziyaretçilerinizi sitenizde tutacak eklentiler ve teknikler » GĂźllĂźo?luweb | Blog Sayfas?()

  • Pingback: Ziyaretçilerinizi sitenizde tutacak eklentiler ve teknikler « internet magazin()

  • 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

  • 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

  • michael

    I love you!

  • rams

    very useful code,

  • Pingback: Plugin for specific search in categories - Bizzteams()

  • Wop


    But how to add a submit button?

    • You would just use your standard search input.

      • Wop



        Could you tell me how?

  • Wop

    I See


  • Thank you it worked for me..

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