WordPress Searching By Category
Sometimes you really want to just search a specific category – more importantly, you might want your visitor to be able to do that.
The generic search is going to search everything, well not exactly since the WordPress search is fairly lousy without some plugin help, but it will work for category searching just fine. In fact, it’s only going to take one line of code to add the functionality to your search, if you’re using a basic theme like the WordPress Default. Custom themes might be more of an issue, so if you get stuck, paste the code that handles your search in the comments and I’ll -try- to help, no guarantees.
Ok, let’s do this with the WordPress Default theme.
The default theme search box should look like this in your sidebar:

The Search Box Before Category Search.
We need to add our line of code to give the users an option to search by categories. So I want you to head over to Appearance -> Editor which will pull up your template files. This will show you the right template files IF you are currently using the WordPress Default theme, if not, use the dropdown box in the right area of the page to switch to the WordPress Default theme.
On the right side of the page, click on the Search Form (searchform.php) file to open that one for editing. The code page you’re viewing inside should look like this:
The code looks like this:
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div><input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
We’re going to add one line as follows:
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div><input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
<?php wp_dropdown_categories('depth=0&orderby=name&hide_empty=1&show_option_all=Search Everything'); ?>
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
So let’s break down what that line is doing:
<?php wp_dropdown_categories('depth=0&orderby=name&hide_empty=1&show_option_all=Search Everything'); ?>
Depth = 0 is setting it so that we’re seeing ALL categories, not just parent or child categories. In another article coming up I’ll show you how to be more specific about categories when dealing with parent/child categories.
Order By = Name is telling them to show in alphabetical order.
Hide Empty = 1 is making sure we don’t see categories that have no posts in them to be searched.
Show Option All = Search Everything is creating an option for them to search all the posts and should be the default.
What do we get?

Search By Category.

Showing Dropdown
Now it’s up to you to style it and make it look pretty
~Nicole




Very nice, thanks =D works great.. but im having problems
with showing the search everything.
any ideas why?
Other than that works nicely.
@Karan
Glad you find it useful
The ‘Search Everything’ option is one that comes from the portion of the code that says: show_option_all=Search Everything
For why it might not work.. not sure. My initial response is always – make sure you’re using a new version of WP. Second to that, I’d say check and make sure you’re not using any kind of scripting or styles that would affect the select options it generates in HTML.
I could more easily tell if I could see a page where this is happening live.
Thank you so much for sharing – I’ve been searching for days for something that maybe you can help me with. I’d like to search all the subcategories of category 10 for example, but I don’t need the dropdown menu. I want just a regular search box that only searches one category and its subcategories. I feel like your method is so close – I added child_of=10 but I don’t want that extra dropdown list.
I would be so grateful for any advice.
Thank you so much for sharing – I’ve been searching for days for something that maybe you can help me with. I’d like to search all the subcategories of category 10 for example, but I don’t need the dropdown menu. I want just a regular search box that only searches one category and its subcategories. I feel like your method is so close – I added child_of=10 but I don’t want that extra dropdown list.
Is it possible to limit the drop down categories at my discretion? I’ve got over 10 categories, but I really only want my drop down to show 4 or 5.
The code works great, but when i try to limit it so it’ll only display the top level categories using depth=1, it continues to display all of the child categories. Any ideas on how to fix it?
Thanks
@Annie
It appears you’ve found a bug in the new version of WordPress. I just replicated the exact same issue in version 2.8.4 and that’s a problem. Somewhere along the line they broke that part of the wp_dropdown_categories function – but I don’t know when.
Another option for the time being might be to just hardcode in your category list that should be generated but isn’t working.
The part of the code that looks like this:
<?php wp_dropdown_categories('depth=0&orderby=name&hide_empty=1&show_option_all=Search Everything'); ?>Generates (on my blog) this:
<select name='cat' id='cat' class='postform'>
<option value='0' selected='selected'>Search Everything</option>
<option value="6">Child Category I</option>
<option value="7">Child Category II</option>
<option value="8">Child Category III</option>
<option value="9">Grandchild Category I</option>
<option value="3">Parent Category I</option>
<option value="4">Parent Category II</option>
<option value="5">Parent Category III</option>
<option value="1">Uncategorized</option>
</select>
You could, until this bug is fix, replace the value=number with the numbers of the categories you want to show and the correct names.
I know it’s not an ideal solution.
@Nicole
Thank you so much! I’ll use that instead
Thanks! That was just what I was looking for. Do you know if there is any way to get this to work with multiple drop downs?
I am trying to use three drop downs but it only seems to recognize the last one and defaults the two before it back to “Search everything”.
I would be grateful for any suggestions.
this only works for me when I select search everything option. When I search in specific categories I get no results.
@Rebecca
To search one category, add a hidden input field to your search form with name of “cat” and value of the id of the category. You can also use something like “if($_GET['cat'] == 3)” to display an appropriate search form in the sidebar, results page, etc.
Worked perfect! This worked better then some of the search plugins I tried.