Do you want to add search by category in WordPress?
Adding a category-specific search to your website can help visitors find what they’re looking for. Visitors can search particular blog categories, product categories, membership levels, and more.
In this guide, we’ll show you how you can add a search by category feature to WordPress.
Why Add Search By Category to WordPress?
If you have a lot of content, then categories can help organize that content.
Categories can also help visitors find interesting posts, for example many sites show their categories in a menu or sidebar.
You may want to go one step further and add a search by category feature to your archive pages or other areas of your site.
Overall, category-specific search gives your visitors a better experience on your site, which will help increase pageviews and reduce bounce rate. This sends positive signals to the search engines and can improve your WordPress SEO.
If you’re using WooCommerce, then you can add a product category search.
This can help visitors find more products to buy on your online store.
If you run a WordPress membership site, then you may use search by category to help visitors find other members, groups, forums, online courses, and other specific kinds of content.
This can help them find what they’re looking for faster, compared to a general search that looks at all the content across your membership site, regardless of category.
With that being said, let’s see how you can add a search by category feature to WordPress.
How to Add Search By Category in WordPress
The easiest way to add a category specific search to WordPress is by using SearchWP.
It is the best search plugin for WordPress and gives you complete control over the search experience on your website.
With SearchWP, you can search content that WordPress ignores by default, including custom post types, PDF files, WooCommerce products, and categories.
First, you need to install and activate the SearchWP plugin. For more details, see our step-by-step guide on how to install a WordPress plugin.
Upon activation, head over to Settings » SearchWP and then click on ‘License.’ You can then enter your license key in the ‘License’ box and click the ‘Activate’ button.
You can find the license key by logging into your SearchWP account.
Once you’ve done that, you’re ready to create a new search engine by clicking on the ‘Engines’ menu option.
This takes you to a screen where you can set the default search engine settings. Think of it like creating your own Google search algorithm that’s just for your WordPress website.
Here, you’ll see a section for the different content types that SearchWP searches by default, such as pages and posts. Each section has its own Application Attribute Relevance sliders which change how SearchWP ranks the content on your WordPress blog.
For example, if you move the ‘Title’ slider to the left and the ‘Content’ slider to the right, then the content will carry more weight than the title.
You can even exclude specific pages, authors, and more from WordPress search.
When you’re happy with how the search engine is set up, click on the ‘Save Engines’ button to store your changes.
At this point you may see a message asking you to rebuild the index. If this happens, then just click on the ‘Rebuild Index’ button.
Next, we’re going to add a category selection option to the search form, so your visitors can decide which category they want to search.
To create this custom WordPress search form, you’ll need to add code to your WordPress website. Often you’ll find code snippets in WordPress tutorials with instructions to add them into your theme’s functions.php file.
However, this isn’t very quick or beginner-friendly. Even worse, a tiny mistake in the custom code snippet can cause common WordPress errors or even break your site completely.
With that in mind, we always recommend you add custom code in WordPress using the WPCode plugin. It is the best code snippet plugin for WordPress, and lets you add custom PHP, CSS, and more with ease.
The first thing you need to do is install and activate the free WPCode plugin. For more details, see our step-by-step guide on how to install a WordPress plugin.
Upon activation, head over to Code Snippets » Add Snippet.
Here, simply hover your mouse over ‘Add Your Custom Code.’
When it appears, click on ‘Use snippet.’
To start, type in a title for the custom code snippet. This can be anything that helps you identify the snippet in the WordPress dashboard.
After that, open the ‘Code Type’ dropdown and select ‘PHP Snippet.’
You can then add the following PHP to the code box:
function my_searchwp_get_block_search_form_with_categories_dropdown( $block_content, $block ) {
if ( 'core/search' !== $block['blockName'] ) {
return $block_content;
}
ob_start(); ?>
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<label>
<span class="screen-reader-text">Search For</span>
<input type="search" class="search-field" placeholder="Search..." value="<?php echo esc_attr( get_search_query() ); ?>" name="s" title="Search for:" />
</label>
<?php
// for more information see http://codex.wordpress.org/Function_Reference/wp_dropdown_categories
$swp_cat_dropdown_args = array(
'show_option_all' => __( 'Any Category' ),
'name' => 'swp_category_limiter',
);
wp_dropdown_categories( $swp_cat_dropdown_args );
?>
<input type="submit" class="search-submit" value="Search" />
</form>
<?php return ob_get_clean();
}
add_filter( 'render_block', 'my_searchwp_get_block_search_form_with_categories_dropdown', 10, 2 );
After that, you’re ready to scroll to the top of the screen and click on the ‘Inactive’ toggle, so it changes to ‘Active.’
Finally, click on ‘Save Snippet’ to make the PHP snippet live.
This code adds a category dropdown menu to all the search forms on your WordPress blog, website, or online marketplace.
Simply visit your website to see the custom search bar in action.
There are lots of other ways you can use SearchWP to deliver more accurate and relevant search results. For more information, please see our step-by-step guide on how to improve WordPress search with SearchWP.
We hoped this article helped you add search by category in WordPress. You may also want to see our expert pick of the best WordPress plugins for businesses, and our comparison of the best email marketing services.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Hansan says
Hey help , I want to create a search box for web page and it should search in only one page category. How can i do that in wordpress.
Eric Thomas says
This is exactly what I was looking for! Needed a sidebar for a separate review section, with a search bar that only searched that separate section. So this was perfect
philips says
Thanks for the support for this awesome plugin. i have tried the plugin, the search by name works fine but the category search is not showing any result. i guess it is not compatible with 3.5 — >3.8.
i have tried changing “cat” to “category” but all to no avail . Any quick help will be hugely appreciated. Thanks in advance.
Eric says
After I installed this plugin, the original categories widget doesn’t work any more!
Once I removed the plugin, it’s work again!
I still can’t find the reason.
Could someone tell me how to fix the problem?
Thanks a lot!
sergio says
i run both regular search with google custom search, and the search category plugin, . i modified the searchform.php in order to have google search results ( with this shortcode ).
When i use the search by category plugin the results are the same of the standard so they are not filtered obviously because i use in both case google custom search .
so i would reset thestandard only for () for this plugin
how can i Do?
thanks
WPBeginner Support says
you can not use search by category and Google custom search. You can create two different forms and search templates.
Admin
Srinivas says
Team,
I tried the steps above, but when i added [sbc] in the text area, it just displayed that as a text instead of calling the search box. Am i missing something here.
Else, also not sure where i can add the mentioned code in the theme template. Could someone help me with the specific theme file i need to edit.
Thanks
Editorial Staff says
Please post all plugin related support in the supports forum:
http://wordpress.org/support/plugin/search-by-category
Admin
Gretchen Louise says
Oh I’m so glad to find this plugin! I think I need to keep looking though because I need to have it search specific pages on my site as well as some categories of posts.
mike says
Really saved my time a lot ………..
This plugin still works with wordpress 3.5.1 !!!!
Rosca Bogdan says
@Alian there is a plugin for that, Query multiple taxonomies.
You can find it @ http://scribu.net/wordpress/query-multiple-taxonomies/ . I use it and it`s working like a charm.
Alan says
Can this be used for search results?
Gitte Hansen says
Search by category is only tested for WordPress version up to 3.4.2 – is it ok to install anyway? I use 3.5.0
I really look forward to a functionality which will make it easier for my readers to browse and seach in my categories.
Greetings Gitte
Editorial Staff says
Yes it is fine. It will work.
Admin
Corrinda says
I just did a review of different options for a client site and eventually installed the Search Everything plugin. It gives you the option to also configure what gets searched. And in this case I wanted to exclude a couple of categories.
Azim says
If no serious customization is needed, you could just use the following snippet in your searchform.php
<form method="get" id="searchform" action="<?php echo home_url(); ?>">
<input type="search" id="s" name="s" />
<?php wp_dropdown_categories( 'show_option_all=All Categories' ); ?>
</form>
Pam says
What would this do for you? tried it but nothing.
Alan says
I use Relevanssi on one of my sites and it is great but I would love to add something like this to narrow search fields but rather than narrow by category. I’d like to narrow by custom taxonomy.