Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Search By Category in WordPress (Best Method)

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.

How to search by category in WordPress (2 ways)

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.

A list of post categories on the WPBeginner website

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.

WordPress archive category search example

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.

Product categories on an eCommerce site

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.

Enter SearchWP license key

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.

The SearchWP default search engine settings

Here, you’ll see a section for the different content types that SearchWP searches by default, such as pages and posts. Each section has an Application Attribute Relevance that will change how the search engine ranks this 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.

Customize search engine weight

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 save your changes.

Save default SearchWP engine

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 customize the search form to add a category selection option, so your visitors can decide which category they want to search.

To do this, 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 or a site-specific plugin.

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 snippets using the WPCode plugin. WPCode makes it easy to add code snippets in WordPress without having to edit your theme’s functions.php file.

For more details, see our step by step guide on how to add custom code in WordPress.

You can then add the following PHP snippet to your website:

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 );

This code adds a category dropdown menu to all of the search forms on your WordPress blog or website.

Select category drop down

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.

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.

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

18 CommentsLeave a Reply

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

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

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

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

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

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

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

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

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

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

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