Improve the search function and find results within a category

Default search function in WordPress is getting better and better with every update. You can easily include search feature on your blog and allow people to easily find the content they want. But if you haven’t made any changes to the function, search results will display everything related to the query. And that might be a problem if you have a lot of content because people will have tons of text on the results pages which won’t help them a lot.

That’s why you should consider searching through titles only or you could add a search box which will go through categories only. In this article, we will show you how to allow your visitors to search only within specific categories which you can define in the code or allow visitors to select ones from the drop-down list.

Select categories in advance:

First, we will show you the code which will create a new search box. In this code, you won’t let people select categories for their search but you will define categories within the code. This might help you with special categories which need a separate search box. For example, you may have a category which contains only smartphone reviews in it. With this code, you can allow people to search only through that category so that the search results don’t load information about home appliances or other stuff you have on the website. Makes sense, right?

  1. Open a page where you want the search box to appear or make a new widget from it (LINK)
  2. Copy and paste the following code:
  3. <form method="get" id="search form" action="/">
    <div>
    <input type="text" value="" name="s" id="s" />
    <input type="hidden" value="1" name="cat" id="scat" />
    <input type="submit" id="search_submit" name="Search" value="Search"/>
    </div>
    </form>
    
  4. Change the value on the third input type to category ID which you want to search
  5. Save changes

After you have made these changes, a new search box will appear and it will allow you to search through category which ID you have entered in the code above. If you want to search through multiple categories, simply add more IDs by entering numbers separated by commas.

<input type="hidden" value="1, 2, 3" name="cat" id="scat" />

While this might be a great solution in some cases, there will be times when you will want to show a list of categories and allow visitors to choose where to search from.

Let visitors choose a category to search from:

Following the same example where you have categories like smartphones, desktop computers, laptops, home appliances, etc. why wouldn’t you allow a visitor to choose one of those and search only through “laptops” category, for example?

In the following lines, we will show you how to do that:

  1. Open sidebar.php or wherever you want the search box to appear
  2. Copy and paste the code:
  3. <form method="get" action="<?php bloginfo('url'); ?>">
    <fieldset>
    <input placeholder="search category" type="text" name="s" value="" maxlength="40" required="required" />
    <select name="category_name">
    <option value="smartphones">Smartphones</option>
    <option value="desktop-computers">Desktop Computers</option>
    <option value="laptops">Laptops</option>
    <option value="home-app">Home Appliances</option>
    </select>
    <button type="submit">Search selected category</button>
    </fieldset>
    </form>
    
  4. Change categories under option values. Write category slug in value and a name between tags
  5. Save changes

Automatically show all categories in the list:

If you want to show all categories and you don’t want to worry about adding or removing ones from this code once you add/delete them in you admin panel, use this code between <select></select> tags:

<?php
$categories = get_categories();
foreach ($categories as $category) {
echo '<option value="', $category->slug, '">', $category->name, "</option>\n";
}
?>

This code will generate a list of all available categories.

And that’s it. Now you can easily add one or more categories which your visitors can easily search through. Whether you want people to select the category themselves or you want to specify those in the code, you are covered.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

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