How To Create Search By Category Functionality In WordPress

How To Create Search By Category Functionality In WordPress

Search by category is a an usual functionality in almost websites that allows you to search in a specific category instead of whole website for more accurate results. It is very useful if your website is well-structured by categories (for example: an e-commerce websites that each category presents one type of products). Unfortunately, this functionality is not implemented in WordPress. In this article, we'll see how to extend the WordPress's default search functionality to allow search by category.

Add list of categories to search form

To search by category, we need to implement the search box first. The search box needs to be added one more field – category – which is shown as a select box.

How To Create Search By Category Functionality In WordPress

This is done simply by adding the function wp_dropdown_categories() into the search form. All you need to do is put the following code into searchform.php file of your theme (this is the file where search form code is stored by default, but not always necessary):

<form id="searchform" method="get" action="<?php echo home_url(); ?>">
    <input type="text" name="s" id="s" size="15" />
    <?php wp_dropdown_categories( 'show_option_none=Select' ); ?>
    <input type="submit" value="Search" />
</form>

If your theme doesn't have the searchform.php file, try to find the code of search form in header.php file (or sidebar.php, it depends on the location of search form in your blog) and then replace it with the code above.

If you want to know more about wp_dropdown_categories() function, you should read the Codex.

After that, refresh your blog, you'll see the select box of categories is shown as in the screenshot.

Add category to search parameters

Try to search with the new search box. You'll see nothing is changed, but the url. The old url contains search parameter (which is "s") only, but the new url contains one more parameter – category (which is "cat"). The new url looks like this:

http://localhost/wp/?s=image&cat=120

Now we need to tell WordPress to understand the category parameter and get proper posts. Add the following code into functions.php file:

add_action( 'pre_get_posts', 'search_by_cat' );

function search_by_cat()
{
    if ( is_search() )
    {
        $cat = empty( $_GET['cat'] ) ? '' : (int) $_GET['cat'];
        add_query_arg( 'cat', $cat );
    }
}

Here we hook into pre_get_posts action. This action is fired when WordPress prepares the query before getting posts from database. Our hooked function do the following actions:

  • check if the current query is search
  • get the category parameter. Note that if user doesn't choose any category (which means search in all categories) then the category parameter is set to -1. So we need to check this to make sure the category parameter is an positive integer or empty string if no category is chosen
  • add the category parameter to current query parameter array

Now you can refresh your blog and try some searchs. Don't forget to choose category to narrow the search results. You'll see the search by category functionality works well as expected.

The search by category functionality is very useful for some types of WordPress blog like a download website or an e-commerce website, which all the content are well-arranged in its category. This feature is helpful for users because they'll find what they need faster and more accurately. Implementing search by category is a good way to improve user experience in your website.

I hope this tutorial is helpful for you. If you have any question, please leave in the comments.

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com

Leave a Reply

Your email address will not be published. Required fields are marked *


Copyright © 2024 Deluxe Blog Tips. All rights reserved.

My products: Meta Box | Slim SEO