
“Search by category” is a an usual functionality in almost websites that allows you to search in a specific category instead of whole website for more accurate results. It is very useful if your website is well-structured by categories (for example: an e-commerce websites that each category presents one type of products). Unfortunately, this functionality is not implemented in WordPress. In this article, we’ll see how to extend the WordPress’s default search functionality to allow search by category.
Add list of categories to search form
To search by category, we need to implement the search box first. The search box needs to be added one more field – category – which is shown as a select box.

This is done simply by adding the function wp_dropdown_categories() into the search form. All you need to do is put the following code into searchform.php file of your theme (this is the file where search form code is stored by default, but not always necessary):
<form id="searchform" method="get" action="<?php bloginfo('url'); ?>">
<input type="text" name="s" id="s" size="15" />
<?php wp_dropdown_categories('show_option_none=Select category'); ?>
<input type="submit" value="Search" />
</form> If your theme doesn’t have the searchform.php file, try to find the code of search form in header.php file (or sidebar.php, it depends on the location of search form in your blog) and then replace it with the code above.
If you want to know more about wp_dropdown_categories() function, you should read the Codex.
After that, refresh your blog, you’ll see the select box of categories is shown as in the screenshot.
Add category to search parameters
Try to search with the new search box. You’ll see nothing is changed, but the url. The old url contains search parameter (which is “s”) only, but the new url contains one more parameter – category (which is “cat”). The new url looks like this:
http://localhost/wp/?s=image&cat=120
Now we need to tell WordPress to understand the category parameter and get proper posts. Add the following code into functions.php file:
add_action('pre_get_posts', 'search_by_cat');
function search_by_cat()
{
global $wp_query;
if (is_search()) {
$cat = intval($_GET['cat']);
$cat = ($cat > 0) ? $cat : '';
$wp_query->query_vars['cat'] = $cat;
}
} Here we hook into pre_get_posts action. This action is fired when WordPress prepares the query before getting posts from database. Our hooked function do the following actions:
- check if the current query is search
- get the category parameter. Note that if user doesn’t choose any category (which means search in all categories) then the category parameter is set to -1. So we need to check this to make sure the category parameter is an positive integer or empty string if no category is chosen
- add the category parameter to current query parameter array
Now you can refresh your blog and try some searchs. Don’t forget to choose category to narrow the search results. You’ll see the search by category functionality works well as expected.
The search by category functionality is very useful for some types of WordPress blog like a download website or an e-commerce website, which all the content are well-arranged in its category. This feature is helpful for users because they’ll find what they need faster and more accurately. Implementing search by category is a good way to improve user experience in your website.
I hope this tutorial is helpful for you. If you have any question, please leave in the comments.


9 comments
Awesome, thnx for sharing. My client requested a ‘search by category’ feature and I didn’t want to go the route of adding another plugin, so this is perfect!
I cannot get the search to work correctly if a category isn’t chosen. It’s working perfectly if a category is chosen but if a category isn’t chosen, the category parameter is set to -1 and no search results appear even though they should.
I’ve added the pre_get_posts action and function to the functions.php file just as it’s written above. This is hosted locally so I cannot share a url with you.
Any ideas?
In the code, the category parameter is set to empty string if no categories were chosen:
$cat = ($cat > 0) ? $cat : '';. You should show the parameters of the current query to see its value (simply hook towp_headand useprint_r($wp_query);)Hi,I have added this on my website.can you check it out if you don’t mind and say if its correct
Great! But there’s something wierd! When I use this in my sidebar from searchform.php i write and then choose category. But when I use the code by itself in my header, it’s unable to search and only shows posts from choosen category :(
Why is that so?!
[...] Another slightly more complicated method is to give your current search box “search by category” functionality. I found this trick at Deluxe Blog Tips. [...]
[...] Another slightly more complicated method is to give your current search box “search by category” functionality. I found this trick at Deluxe Blog Tips. [...]
[...] Another slightly more complicated method is to give your current search box “search by category” functionality. I found this trick at Deluxe Blog Tips. [...]
[...] Another slightly more complicated method is to give your current search box “search by category” functionality. I found this trick at Deluxe Blog Tips. [...]
[...] Another slightly more complicated method is to give your current search box “search by category” functionality. I found this trick at Deluxe Blog Tips. [...]