Wordpress Single Category Search

custom-search

Sometimes it comes in handy to make the search form on a blog only return results from a single category. It’s actually quite simple.

1. Find the category ID

First we need to find the ID of the category that we want to search. We can find that by going to Posts>Categories and hovering over the category we want to search in the categories list on the right side of the page. When we hover the mouse over a category, we’ll see something like this in the status bar (usually the bottom left corner of the browser):

…/wp-admin/categories.php?action=edit&cat_ID=3

As you can probably see, the 3 at the end is the category ID. That number is all we need to know.

2. Edit the searchform.php file

We just need to find this line in the searchform.php file of the theme:

<form method="get" id="searchForm" action="<?php bloginfo('home'); ?>/">

and add this hidden field immediately after it:

<input type="hidden" name="cat" id="cat" value="3"/>

3. Apply the category ID

Now we need to make sure that we change the value of the hidden field to the category ID we found in step one. For example, if the category we chose had an ID of 12, then the hidden field would look like this:

<input type="hidden" name="cat" id="cat" value="12"/>

It’s that easy! If you have any questions, feel free to post them in the comments section below.

Image by rockmixer

Category: Wordpress Follow comments with RSS 2.0

6 Responses to “Wordpress Single Category Search”

  1. Brian Purkiss | September 7, 2009 at 10:18 am

    Dang!
    That’s a great WP ‘hack!’
    I am definitely going to have to use that!

    What code highlighter are you using? It’s nice.

    (fyi, you have a broken link in your footer.)

  2. alexmansfield | September 7, 2009 at 12:14 pm

    Thanks for the heads up on the footer link.

    I’m using SyntaxHighlighter Plus, but I edited the stylesheet a little. It’s been working great for me, however, it’s no longer maintained. You might want to check out SyntaxHighlighter Evolved instead (http://wordpress.org/extend/plugins/syntaxhighlighter/).

  3. shawn | October 24, 2009 at 2:06 pm

    How would I go about doing this dynamically?

    Meaning a person is viewing either a category archive page, or a single article and wants to search for other articles within the category page they are viewing.

    So I could have a search box on the sidebar, where if a person is viewing cat ‘abc’ it would search cat ‘abc’ and if a person is viewing an article or archive for cat ‘xyz’ they would get to search ‘xyz’..

    Basically dumping the hardcode and replacing with a dynamic variable determined via the cat displayed.

  4. alexmansfield | October 30, 2009 at 12:31 am

    Interesting idea. I haven’t actually tried this, but here’s how I think I would go about it. First, I would grab the category with the get_the_category() function (http://codex.wordpress.org/Function_Reference/get_the_category). This returns an array of the categories belonging to that post. Then I would extract the first category of the array and use that instead of the hard coded category. I’m not really sure if I explained that very well or not. Let me know how it goes.

  5. WPExplorer | June 14, 2010 at 1:01 am

    This is really helpful. I was trying to limit my search bar so results from my blog would not show up…works great. thanks!

  6. alexmansfield | June 14, 2010 at 3:04 am

    You’re welcome. I’m glad you found it useful!

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