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

  • http://www.wiflba.com mc2w

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

  • http://www.seoconsultant.ie 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?

  • http://vhmorales.com.ar viktor

    Category 1
    Category 2

    I guess…

    :)

  • http://vhmorales.com.ar viktor

    Ups… didn’t post the code…

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

  • Adriana

    Great tip. Thank you!

  • http://wp123.info Levani

    Doesn’t work for me…

  • http://thebigfelafel.com Rebecca

    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:
    http://wordpress.org/support/topic/266658?replies=2#post-1065708

    • http://www.sdacinc.com 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://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”

  • http://xzombi.com xzombi

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

  • http://gabrielecripezzi.com 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!

  • http://bestbeeronline.com Tony

    Thanks! You are a lifesaver.

  • http://www.brokencode.com brokencode

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

  • http://www.mikkosaari.fi/relevanssi/ 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.

  • http://www.coolzrock.org.uk Coolzrock

    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

  • http://itmdesign.co.uk ITMDesign

    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

  • http://www.women.it 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

  • http://www.ewillys.com 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

  • http://seo.com Travis W

    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

    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

  • Michel

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

    and so on so forth.
    Thanks,
    M.

  • Laveni

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

  • http://imod.co.za 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!

  • Alek

    Doesn’t work. At least not with wordpress 2.6

    • Alek

      I really meant… 2.8.6

  • Alek
  • http://xiawa.my Xiawa

    great tutorial, many thanks :)

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

  • Pingback: voragine.net » Blog Archive » Limitar el buscador de WordPress a ciertas categorías

  • http://thisisdeliberate.co.uk Michael Bailey

    Brilliant! Thank you :)

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

  • Pingback: Doug Hogan » weekly life

  • http://www.gradientgraphics.net/ 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!

  • Pingback: WordPress?????????? | Wordpress???

  • Pingback: 8 useful receipes to improve your WordPress theme

  • http://www.dreamarchive.org 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.

    • http://www.sdacinc.com 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?

      • http://www.dreamarchive.org 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!

  • http://www.awidea.info awidea

    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

  • http://descargar-ares.org/ jonh | descargar-ares

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

  • http://djrajobos.org/ anton | djrajobos

    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
    http://webstutorial.com/wordpress-multiple-category-search/content-management-system-cms/wordpress-cms

  • michael

    I love you!

  • rams

    very useful code,
    thanks

  • Pingback: Plugin for specific search in categories - Bizzteams

  • http://www.usa4all.nl Wop

    Works!

    But how to add a submit button?

    • http://www.sandboxdev.com Jennifer Zelazny

      You would just use your standard search input.

      • Wop

        Hi,

        Tx,

        Could you tell me how?

        • http://www.sandboxdev.com Jennifer Zelazny

          I have added in the search button to the example above.

  • Wop

    I See

    TX!!

  • http://karnataka360virtualtour.com Manjesh V

    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.

    • https://plus.google.com/u/0/108255828319170525156/posts Jennifer Zelazny

      This still works

Translate
Categories
Archives