Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Search By Category in WordPress (Best Method)

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

Do you want to enable search by category in WordPress?

Adding a category-specific search to your website can help visitors find what they’re looking for, no matter what content you publish. For example, visitors might search particular blog categories, product categories, membership levels, and more.

In this guide, we will show you how you can add a search by category feature to WordPress.

How to search by category in WordPress (2 ways)

Why Add Search By Category to WordPress?

Categories are an easy way to organize the content on your WordPress website. Categories can also help visitors find interesting posts.

For example, many sites show their categories in a dropdown menu or sidebar.

A list of post categories on the WPBeginner website

If you have lots of blog posts, then you can go one step further and add a search by category feature to your website. This can help visitors find the content they’re looking for, even if you have lots of blog posts.

If you are using WooCommerce, then you can even create a product category search for your online store and get more sales.

Product categories on an eCommerce site

Similarly, if you run a WordPress membership site, then you might use search by category to help visitors find specific members, groups, forums, online courses, and more.

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 and allows you to improve the built-in WordPress search.

With SearchWP, you can search content that WordPress ignores by default, including custom post types, PDF files, WooCommerce products, and categories.

To learn more, see our detailed SearchWP review.

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, go to SearchWP » Settings and then click on the ‘General’ tab.

Activating the SearchWP WordPress plugin

Here, you need to enter your license in the ‘License Key’ box. You can find this information under your account on the SearchWP site. Once you are done, make sure to click the ‘Verify Key’ button.

Create a Custom Search Algorithm

After activating the plugin, you can fine-tune how SearchWP indexes, searches, and displays your content. You can think of this as creating a custom Google search algorithm that’s just for your WordPress blog or website.

To get started, head over to SearchWP » Algorithm.

Creating a search by category feature for WordPress

Here, you’ll see a section for the different content types that SearchWP searches by default, such as pages, posts, and media files. Each section has its own Application Attribute Relevance sliders, which allows you to control how SearchWP ranks the content on your online marketplace, blog, or website.

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.

Changing the attribute relevancy settings in WordPress

You can even exclude specific pages, authors, and more from WordPress search.

Another option is to include more content in the search results by clicking on the ‘Sources & Settings’ button.

Configuring your website's search algorithm using SearchWP

You can now check the box next to the different content types. For example, you may want to make blog comments searchable or add PDF indexing and searching in WordPress.

When you are happy with your changes, click on the ‘Done’ button.

Making comments, PDFs, media, and more searchable on a WordPress website

This adds a new section for each new content type. You can now change the attribute relevance using the different sliders.

Once you are happy with how the search is set up, scroll to the top of the screen and click the ‘Save’ button.

Saving your SearchWP search engine settings

SearchWP will now rebuild the index automatically. Depending on the speed of your Internet connection and your WordPress hosting provider, this may take a few minutes.

As soon as you see ‘Index Status 100%,’ you’ll know that SearchWP has indexed all your content and settings.

Rebuilding the search index on your WordPress website

Add as Search By Category Feature to WordPress

After that, you’re ready to add a category selection option to the search form. This will allow visitors to choose the category they want to search.

Select category drop down

To create this custom WordPress search form, you’ll need to add code to your WordPress website.

Often WordPress tutorials will ask you to add custom code to your theme’s functions.php file. However, this isn’t easy, and even 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.

How to create a custom search form using WPCode

You will now see all the ready-made WPCode snippets you can add to your site. These include a snippet that allows you to completely disable comments, upload file types that WordPress doesn’t usually support, disable attachment pages, and much more.

Here, simply hover your mouse over ‘Add Your Custom Code’ and select ‘Use snippet.’

Customizing the search form using WPCode

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

Adding a custom PHP snippet to WordPress

You can then add the following custom code in WordPress:

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 are ready to scroll to the top of the screen and click on the ‘Inactive’ toggle so that it changes to ‘Active.’

Finally, click on ‘Save Snippet’ to make the PHP snippet live.

Saving a code snippet with WPCode

This code adds a category dropdown menu to all the search forms on your WordPress website. Simply visit your website to see the custom search bar in action.

With that done, you may want to continue improving the WordPress search feature by adding live Ajax search. You can also see our guide on how WordPress search works and tips to make it better.

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 SEO tools and plugins that you should use or our guide on how to increase your blog traffic.

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.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

19 CommentsLeave a Reply

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

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

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

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

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

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

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

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

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

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

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

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

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.