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

WordPress Default Search Form Editing.
WordPress Default Search Form Editing.

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?

The Final Search By Category view for the WordPress Default.
Search By Category.

Showing Dropdown
Showing Dropdown

Now it’s up to you to style it and make it look pretty 🙂

~Nicole

23 Replies to “WordPress Searching By Category”

  1. Very nice, thanks =D works great.. but im having problems
    with showing the search everything.

    any ideas why?

    Other than that works nicely.

  2. @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.

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

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

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

  6. Brian – You can add the exclude or exclude_tree option to the wp_dropdown_categories code by placing another separating & symbol and then the exclude call with the numbers of the categories to exclude.

    The link above explains the difference between exclude and exclude_tree, but in a nutshell the exclude_tree will remove a parent category and all it’s children instead of having to list out all the child categories as well.

    Was trying to post code in my comment, but for some reason it’s not letting the comment post correctly so I took it out. Will have to check that out later to see what’s going on.

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

  8. @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.

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

  10. this only works for me when I select search everything option. When I search in specific categories I get no results.

  11. @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.

  12. Hi,

    To search top level categories only, set depth=1 but to make this work you also need to set hierarchical=1 or depth is ignored. Set depth=2 if you want sub categories, and so on.

    Code should be:
    wp_dropdown_categories('hierarchical=1&depth=1&orderby=name&hide_empty=1&show_option_all=All Categories');

  13. nice hacks, anyway, can we give separated search result with different url, i mean, like
    domain.com/search/category/search-result
    domain.com/search/category2/search-result

  14. Hi Nicole

    This is what I’m looking for. It’s useful so that people don’t get lots of irrelevant posts. Im a little stuck and new to wordpress. I’m using the MystiqueR2 theme and my search form code looks like this:

    <?php
    if(($searchquery) && ($searchquery!=__('Search',"mystique"))):
    printf(__("You have searched the archives for %s","mystique"), '‘.wp_specialchars(get_search_query(),1).’‘);
    else:
    _e(“Type a valid search term…”,”mystique”);
    endif;

    ?>

    I can’t see where I’d add your code into this. Can you help?

    Thanks

  15. Hi Nicole

    I don’t think all my code pasted in. Here it is again:

    <?php
    if(($searchquery) && ($searchquery!=__('Search',"mystique"))):
    printf(__("You have searched the archives for %s","mystique"), '‘.wp_specialchars(get_search_query(),1).’‘);
    else:
    _e(“Type a valid search term…”,”mystique”);
    endif;

    ?>

  16. Hi, wonder if you could help. I’m using this to show a drop down of categories from a custom taxonomy, this works, however, the search results produce a 404 because it is trying to display results with cat=30 (or whatever number it is). It actually needs to show the cat name instead, do you know how to do this?

  17. Hi Nicole,

    It looks great! Just had a couple of questions. In my drop down…all I see is “uncategorized”. What is this referring too…to give you some background on this …I’m using this feature in the header and adding the code to an advance search that required…text entry…however, I wanted a way in which a user could see there particular item via drop down…the reason I intalled your code is unconvention..or out of the box..I need a way in which a user can search a particular category from an auction website…in my case…a particular charity associated with an item…can this code do this..or am I barking up the wrong tree…you help is greatly appreciated…

Comments are closed.