SearchWP

News

WordPress search by category

Trying to add search by category to your WordPress site?

WordPress blog and product categories help you organize your site content. By letting your users search specific categories, you can help them find blog posts and pages faster.

In this article, we’ll show you how to add the search by category feature to WordPress.

Why Add Search by Category to Your Website?

Do you have a website with lots of content covering many topics? A search by category form can help your site visitors find what they need faster.

As your site continues to grow, it may become harder for your users to find the specific content they need. You can solve this by allowing them to limit their search to specific categories.

search by category in WordPress

So they can now search within an individual section instead of your entire site. This can help them find more relevant results related to their topic of interest.

For example, a user can search for a certain keyword in the ‘Tutorials’ category. This will help your site narrow down the results to content related to tutorials only.

Search by category in WordPress results

You can add this search feature to your archive pages or knowledgebase. This way, your visitors won’t have to scroll through tons of irrelevant content before finding what they need. They can also use it to find older content faster.

If you own a large site, you can use this feature to offer your customers a better user experience. This will lead to higher conversion rates, lower bounce rates, and even better SEO performance.

WordPress supports categories and subcategories by default. But your users won’t be able to search individual categories to narrow their results.

To add category search to WordPress, you can either use a WordPress plugin or code a search form from scratch. The first method requires little technical experience and will save you a lot of time.

We’ll show you how to use a WordPress plugin to create a search by category form below.

How to Add Search by Category to WordPress (Fastest Method)

The fastest way to add a category search to your WordPress site is to use a plugin like SearchWP.

SearchWP best WordPress search plugin

SearchWP is the best WordPress search plugin. Used by over 30,000 sites, it allows you to add a search by category feature to WordPress with little to no prior coding experience.

With this plugin, your site visitors get a lot of control over their search results. It adds a dropdown menu of categories for the user to select from when searching.

You also get to enjoy extra features like:

  • Keyword stemming. SearchWP can search for matches related to the keywords you enter. For example, it can show results for terms like ‘running’ and ‘runner’ when you search for ‘run’. The plugin allows you to control how this works.
  • Multiple search engines. You can customize the search experience on different pages of your site. Choose which attributes to consider and how relevant you want them to be.
  • Search insights. Keep track of what your site visitors are searching for and discover new ways to serve them better.

Now, let’s talk about the steps you need to follow to add the category search feature.

Step 1: Install and Activate SearchWP

To add category search to WordPress, you need to first get a copy of the SearchWP plugin here.

After grabbing your copy of the SearchWP plugin, you’ll find links to 2 versions of the plugin.

Click any of them to download the plugin. We recommend using the latest version for the best performance possible.

SearchWP will also send you an email with your license key. You can save it in a notepad for future reference. You’ll enter this key in your WordPress dashboard when it’s time to activate the plugin.

Next, go to your WordPress dashboard and upload the plugin to your site.

If you need help with that, you can check this detailed guide on how to install a WordPress plugin.

After installing and activating the plugin, you also need to activate the license.

To do this, navigate to the SearchWP settings page by clicking the button in the top panel of your dashboard.

go to SearchWP settings page

Go to the License tab and fill in the license key from the previous email. Then click Activate.

Activate SearchWP license

The SearchWP plugin is now fully activated and ready for use.

Step 2: Copy and Paste the Category Search Code to Your Site

The next step is to add some code to your WordPress site. This code will modify your search forms and allow the user to select from a list of categories when completing their search.

The easiest way to add custom code to WordPress is to use a plugin like WPCode. The plugin saves you the stress of adding code manually to your theme’s functions.php file. Follow this guide on how to install a WordPress plugin to add the plugin to WordPress.

Upon activation, the plugin will add a new tab to your WordPress dashboard. Go to Snippets and click Add New.

Add new WordPress code snippet

Next, add a title for your new code snippet. Then copy and paste the code below into the Code field.

function my_searchwp_get_search_form_with_categories_dropdown( $form ) {
	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( 'get_search_form', 'my_searchwp_get_search_form_with_categories_dropdown' );

WordPress search by category code snippet

You can add a description and tags to make it easy to remember what the snippet does in the future. Click Save Changes and Activate when you’re done.

Add description and save changes to WordPress code snippet

If you want to add search forms to widgets or pages with the block editor, then use the code below instead.

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

You can also add both code snippets to ensure the feature works every time you add a search field.

Now when your site visitors use any search form on your site, they’ll see a category dropdown. They can then select any category to limit their search to this category.

WordPress search by category in action

In this article, you’ve learned how to use SearchWP to add the search by category feature to WordPress.

With this feature, your site visitors can find products and posts faster by limiting their search to individual categories.

Ready to use the best WordPress search plugin? Get started with SearchWP here.

You may also want to learn more about how to add a custom WooCommerce search widget.

Want to make your search awesome right now?

More than 30,000 sites have chosen SearchWP!

You can utilize all of the content that’s gone unrecognized by native WordPress keyword search instantly with SearchWP.

Get SearchWP for just $99

  • Committed Support
    If you need help, support is fast, friendly, and here for you
  • Streamlined Setup
    Installation and setup that’s optimized for speed
  • Great Documentation
    Helpful, clear, and usable documentation is a priority

See what SearchWP customers have to say

  • “I purchased SearchWP for its ability to search inside more than 100 PDF documents on my website. My site visitors are able to see search results with pages, posts and PDFs weighted in my preferred order. Customer support has been very helpful.”

  • “With SearchWP you can tweak your search results so you can better target the needs of your site visitors. You have the power to expand your search into custom fields, target specific areas of your site such as taxonomies, post types, and on and on.”

  • “The power, control, and best-practices coding approaches upon which SearchWP is built make it the best search feature replacement for WordPress I have ever implemented. The people behind this plugin are simply outstanding, and have earned my loyalty.”