How to Search By Category in WordPress

Affiliate Disclaimer: This site is supported by its visitors. We may earn a commission for purchases made through the links on our site, this helps to keep everything running smoothly. Learn More

Last Updated on November 24, 2022

A category-specific search on your site can assist visitors in locating what they’re looking for.

In this tutorial, we’ll explain how to add the search-by-category feature to WordPress.

Why Add Search By Category to WordPress?

If you’re a huge collector of content, categories can assist in organizing that content.

Categories can help users discover interesting content For instance, many websites display their categories in the form of a menu or a sidebar.

It is possible to take it one step further and include the search feature by category on your archive pages or other areas on your website.

In general, a category-specific search provides your users with a better experience on your website and will increase the number of page views and decrease bounce rates. It sends positive signals to the search engines and will improve the quality of your WordPress SEO.

If your WooCommerce is installed, you’ll be able to add the ability to search for categories of products.

This will help customers discover more items to purchase from your store online.

Let’s look at how you can implement the ability to search by category in WordPress.

How to Add Search By Category to your WordPress site

The most straightforward method to add a specific category search to WordPress is to use SearchWP.

It’s the most effective search engine plugin for WordPress that gives users total control over the experience of searching on your site.

With SearchWP, you can find content that WordPress does not allow by default, such as custom post types and PDF files, categories, and WooCommerce products.

The first step is to download and then activate the plugin.

After activation, go to Settings > SearchWP and click “License”.

Then, enter your license number in the box labelled ‘License’ and then click the button to activate.

After this, you’re now ready to start a new search engine by selecting the menu item Engines.

There’s an area for the various kinds of content that SearchWP will search for regularly, including posts and pages. Each section is tagged with the Application Attribute Relevance, which will alter the way that SearchWP evaluates the content for the WordPress blog.

For instance, if you shift the Content slider to the left, then the content will have more importance over the name.

You can also exclude certain pages or authors by excluding them from WordPress search.

If you are satisfied with the way the search engine has been set up, click on the Save Engines button to save the changes.

In this instance, you might receive a notification asking you to build the index. If that happens, simply click the button ‘Rebuild Index.

Modifying the Search Form

The next step is to modify the search form to include the option to select a category to allow your users to choose the category they would like to look up.

To do this, you’ll have to add some custom code to your site.

A lot of times, you’ll find code snippets within WordPress tutorials that provide instructions on how to include them in the theme’s functions.php file or in a specific plugin for your site.

But it’s not very easy or user-friendly. And even a small error in the code snippet you’ve created can result in common WordPress mistakes or even destroy your website completely.

We generally would suggest that you include custom code snippets using WPCode. WPCode allows you to include code snippets into WordPress without the need to modify the WordPress theme’s functions.php file.

Then, you can include the following PHP code to your site:

function my_searchwp_get_block_search_form_with_categories_dropdown( $block_content, $block ) {
If ( “core/search”= $block[‘blockName’$block[‘blockName’] ) {*
return $block_content;
}
ob_start(); ?>
Search For
?php return the ob_get_clean();
}
add_filter( ‘render_block’, ‘my_searchwp_get_block_search_form_with_categories_dropdown’, 10, 2 );

This code will add the category dropdown menu on every search form that you have on the WordPress blog or your website.

Hope this helped!

More Posts