How to modify search features of your WordPress website

You will need to modify the search features of your WordPress website once you start writing and accumulate more than just a few articles. This will be helpful as your traffic grows for your website. Search features will help your visitors look for the articles thy are particularly interested in as they will certainly not be interested in all the posts you have made. The search feature is a must-have option for every blog and especially for those that publish content on regular basis. and give your users the power of searching through your website.

Read more WordPress tutorials:

7 Security Tips for a WordPress Website

Automatic Update in WordPress

Changing your WordPress website location

Configuring the basic settings in your WordPress website

The same way people search on Google to get to your site, you want them to be able to search through your blog as well. In WordPress, there is a default search feature. But if you have tried working with it, you have probably noticed it is not as good as you imagined, even though it has improved over the years.

So, this article will take you through the steps that can help you improve your user search experience in WordPress. Here are the areas covered in this post:

  • Best search plugins for WordPress
  • How to change default search URL slug
  • How to search posts exclusively by their titles
  • How to improve the search function and find results within a specific category
  • How to define categories for WordPress search results
  • How to exclude all pages or posts
  • How to exclude only some pages or posts
  • How to allow usage of Voice Search in WordPress

Best search plugins for WordPress

WordPress has a variety of plugins, with plugins, you shouldn’t worry about how to modify search function. The numerous plugins will extend your search possibilities and you will proudly present your search box on the site you own.

If you do everything right, users will likely type what they want in your new search box and get results they expected. Now let’s look at some of the best WordPress search plugins you can use on your website.

Also read more WordPress tutorials:

How to Backup Your WordPress Website Automaticaly Using Backup Plugins

How to Fix a Hacked WordPress Website

How to Install and Setup Your Premium WordPress Theme

How to Keep Your WordPress Website Updated

Cost: PRICE: Free

Let’s start with a plugin which will help you get the power of Google search engine on your website. This free plugin will help you set up a Google search box which you can easily place on your website as a widget.

You will have to register a Google Custom search account, get the ID and the plugin will do the rest. Then, your visitors will be able to search your site or the entire web, depending on your settings, or even search only for images if you decide to do so. You can customize many search features, apply custom layouts, change colors, fonts, thumbnails and much more.

Cost: Free

This modern plugin is already used by many popular websites which recognized its power. Not only Swiftype features one of the best search algorithms which will give you meaningful results, but it gives you a detailed insight into all searches made on your site. With this feature, you can easily find the best keywords for your website and make the content and your search results even better.

The plugin will replace standard WordPress search, and once you have created a free account you can customize Swiftype to make it as best as it can be for your site. If you have a mobile application that displays content from your blog, you can help your mobile visitors search the content even easier with the mobile SDK.

Cost: From $49

SearchWP is another popular WordPress plugin and a good substitute for the default WP search. After the installation, you won’t have to deal with a difficult setup, but instead, you can configure it to work with custom fields and post types, different taxonomies, etc.

The plugin works great with different media types so it will easily find videos, PDF documents, audio and much more. Its algorithms take relevance of keywords in your articles Developers like the possibilities of extending the plugin’s functionalities, while you can also install different extensions.

You can expect detailed statistics of search results so you can work on content people are searching for.

Also read more WordPress tutorials:

How to Manage 403 Forbidden Error in WordPress

How to Optimize and Speed Up Your WordPress Website

How to Secure a WordPress Website

How to control new user registration in WordPress

Cost: Free/ from $49.95

Relevanssi is a Finnish word for relevant. This plugin will replace your default WordPress search engine and make it quite better. Relevanssi will sort your results according to its relevance, not the date. And that’s just the first feature on the long list. This powerful plugin can match partial words (fuzzy matching), it will allow you to search for phrases or let you search for just one term.

Relevansi allows ou to create custom excerpts, highlight the results in documents, and do much, much more. Relevanssi can search through comments, tags, and custom post types. It will also give you the power to select the weight for titles, tags, and comments and thus improve searching.

One of the numerous features we would like to mention is the suggestion function which will give users similar search queries – something like Google’s well known “Did you mean?” suggestion. Relevanssi offers much more, and this is only a free version that we’re talking about. If you need more, especially if you’re running a multisite, you should take a look at a premium version.

Cost: Free / $159/month

SearchIQ is another great WordPress search plugin that provides an enhanced search experience to the users. With powerful search logic, advanced semantic analysis, error handling, and many such features, SearchIQ direct users to the results they were looking for. SearchIQ is quite easy to install and customize.

Getting started requires to create an account on SearchIQ, install and activate the plugin, copy the API key from SearchIQ dashboard, and enter API key in wp backend. Free version is available with basic features and for businesses in need of advanced features such as PDF, CDN, Facet, or custom branding, paid packages can be subscribed.

Not only can you search across pages, posts, WordPress default system fields, user defined custom fields, product attributes and taxonomies, PDFs, and excerpt. But SearchIQ’s dashboard also includes real-time analytics data that helps you to understand your user’s search behavior so you can optimize your content strategy better.

Another great free substitute for your default WP search is Search Everything plugin. It will improve your search results without modifying templates. You can set it up to search almost everything in your WordPress including pages, excerpts, drafts, attachments, comments, tags and even custom fields. Besides that, you are free to leave out pages and post you don’t want to be found in the search results.

This plugin comes with a little helper named Research Everything which will search for your content while you write a new article and will give you the ability to link to other content immediately. If you want to research even more, Power Search feature will help you find content on the Internet. If you’re in the run, simply install and activate the plugin without even looking at the settings page and you’ll have an improved search engine up and running in a matter of minutes.

Also read more WordPress tutorials:

How to create a simple portfolio website with WordPress

How to create and manage a page in WordPress

How to safely disable the WordPress automatic update feature

How to update your WordPress installation

Change default search URL slug in WordPress

Unless your website is a one-pager which only contains the name of your company and contact info, a search feature is a must-have feature. It doesn’t matter if you are just starting a blog or already have thousands of posts and pages on your website, whether you write as a hobby or make money from your blog, you have to do everything to keep your visitors on the site. Giving them an easy and quick way of searching through your website is definitely one of the crucial things you should take care of.

While there are many things you can do about your search feature, you may have forgotten about one simple yet important thing. That is URL slug which appears in the address bar once search returned the result page.

By default, WordPress uses a non-user-friendly version of the slug. Not only your visitor will find that slug weird, but it may harm your SEO as well. Better to say, you can help your SEO by changing the slug into something understandable both to humans and machines.

If you have tried searching through your website, you could have seen that strange /?s=term slug which appears in the address bar. In this part, we will show you how to change that into anything you want. The following actions are required.

Open functions.php
Copy and paste the following:

function feed_dir_rewrite( $wp_rewrite ) {
$feed_rules = array( 'search/(.+)' => 'index.php?s=' . $wp_rewrite->preg_index(1));
$wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
add_filter( 'generate_rewrite_rules', 'feed_dir_rewrite' );

Change destination URL on the third line to whatever you like and save changes

After you’ve done these changes to your functions.php file, you can try out your new URL slug. Don’t worry; if a plugin or a user tried to get access to your search result by navigating to old URL slug, your website won’t return an error but simply redirect the search result to the new URL.

If you want to change the slug to something unique, you can do that on the third line of the code.

This one will take your search results page into www.yourdomain.com/search/search-term

Now you can relax about how search results are displayed to your users as well as to search engine bots who will now more easily crawl through those results.

Search posts exclusively by their titles

If you’re still using default WordPress search engine, sooner or later you will want to modify how it works. While there’s nothing wrong in the way it searches for your posts, you might want to limit the search to titles.

Instead of going through all of the content, this little function will search only through post titles. In some cases, this might really help you get cleaner and faster search results:

Open functions.php
Copy and paste this code:

function __search_by_title_only( $search, &$wp_query )
{
global $wpdb;
if(empty($search)) {
return $search; // skip processing - no search term in query
}
$q = $wp_query->query_vars;
$n = !empty($q['exact']) ? '' : '%';
$search =
$searchand = '';
foreach ((array)$q['search_terms'] as $term) {
$term = esc_sql($wpdb->esc_like($term));
$search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
$searchand = ' AND ';
}
if (!empty($search)) {
$search = " AND ({$search}) ";
if (!is_user_logged_in())
$search .= " AND ($wpdb->posts.post_password = '') ";
}
return $search;
}
add_filter('posts_search', '__search_by_title_only', 500, 2);

Save changes

After the changes, you can open your website and start a new search. To be sure everything’s working as it should, you should try searching for a word you know is in one of your post titles which should return the post in the results page. If you try to search for a word or a complete term that isn’t contained in any title which you have published, search results will come out empty.

And that’s actually all there is. If you ever decide you want the old search back, simply erase or comment out the function.

Also read more WordPress tutorials:

The Many Uses to Which You Can Put Your WordPress Website

What you should know about optimizing your WordPress website for speed

WordPress Search Engine Optimization Tutorial

WordPress Website Speed Optimization Tutorial

Improve the search function and find results within a category

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

Improve search function WordPress

Now you need to know 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. With 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?

Open a page where you want the search box to appear or make a new widget from it
Copy and paste the following code:

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

Change the value on the third input type to category ID which you want to search
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:

Open sidebar.php or wherever you want the search box to appear
Copy and paste the code:

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

Change categories under option values. Write category slug in value and a name between tags
Save changes

Automatically show all categories on 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 your 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.
Define categories for WordPress search results

By default, WordPress searches for everything on your site. If a user types a word or the entire phrase in a search box, he will get results from the entire website and that includes posts, pages, categories and tags, titles and content, etc. If you haven’t installed any other search system, or already have made changes, you may want to modify the default search box.

Search category in WordPress

If you want to modify the search box which already exists in your WordPress and define categories which will be included in the result pages, you may take a little different approach.

In this part, we’re about to show you how to do that. You can easily add or remove categories from the code and decide which one you (don’t) want in your search results.
Add or remove categories from search results:

Open functions.php
Copy and paste the code.

function searchcategory($query) {
if ($query->is_search) {
$query->set('cat','1,4,6');
}
return $query;
}
add_filter('pre_get_posts','searchcategory');

Add categories’ ID on the 3rd line
Save changes

Now that you have made the changes, search results will only get information from specified categories while the other ones will be excluded. As you can see, you can easily add categories on the list; you can have only one category if you want to, or you can set as many as you’d like your visitors to be able to search through. Simply write category IDs separated by commas.

If your only goal is to exclude a category or two from the search results, you can do it much quicker; instead of typing in all the categories you want to include, you can exclude one by typing a minus (-) sign in front of the category ID. For example, if you want to exclude a category which has ID=1, the third line of code would look like this:

$query->set('cat','-1');

There you go. Now you can manage your search results more efficiently.
Exclude pages or posts from your WordPress search

In most cases, you will want your visitors to be able to search through your posts while results coming from pages might only distract a typical “searcher”. If your pages contain contact information, maps, author info, etc. you probably don’t need those pages to be included in the search results.

How to exclude pages from WordPress search

While there is no easy way of telling WordPress to exclude a post type from search results, you will need to that by yourself:

Open functions.php
Copy and paste the following code into the file:

function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'page');
}
return $query;
}

add_filter('pre_get_posts','SearchFilter');

Save changes

After you have made the changes and save them, you are free to visit your website and test the search engine. If everything’s ok, you shouldn’t be able to find any information from your pages in the search results, i.e., you successfully exclude pages from WordPress search results.

How to exclude posts from WordPress search

In the introduction, we told you that WordPress goes through posts and pages and assumed you don’t want pages to show in the results. But what if you don’t want posts to be included in those results? If so, you can make one simple change to the code and exclude posts instead of pages:

Again, you will need to repeat those 3 simple steps shown above, but with a simple change in line #3. Instead of setting post type to “page”, you have to set it up to “posts”:

$query->set('post_type', 'posts');

Don’t forget to save everything and you’re done.

Exclude pages/posts from search results with a plugin

Instead of working with the code, we will show you a plugin which can do the same job for you and make everything much more user-friendly.

PRICE: Free

The plugin we have used for the job is Search Exclude. Using the plugin couldn’t be easier:

  1. Navigate to Plugins->Add New
  2. Search for “Search Exclude”
  3. Install and activate the plugin
  4. Go to any of your posts and pages
  5. On the right side panel, find a “Search Exclude” checkbox where you can decide whether you want the post/page to be excluded from your search results or not.


Once you have excluded several posts/pages, you can easily find the list of excluded items in the plugin settings (Settings->Search Exclude). On the list, you can again use the checkbox to change the status of a post/page.
How to exclude only some pages/posts from your search results

It doesn’t matter if it’s only one page, two or ten which you want to exclude from showing in the search results, you can do that easily by implementing the code. Before you rush copy-pasting the code into your functions.php file, we suggest you prepare the pages which you don’t want to be featured in the search results. You will need page IDs:

Go to Pages->All Pages
Find the page you want to exclude and click “Edit”
In the address bar, locate the “post=6” or something similar where the number will represent a page ID

After you have prepared pages’ ID numbers, you are ready for the code:
Go to function.php file and open it
Copy and paste the following code:

function jp_search_filter( $query ) {
if ( $query->is_search && $query->is_main_query() ) {
$query->set( 'post__not_in', array( 1,2,3,4,5,6 ) );
}
}

add_action( 'pre_get_posts', 'jp_search_filter' );

On line #3, change the numbers in the brackets to ID numbers which you have read from the page. In this example, we have excluded 6 pages with their respective IDs 1 to 6.
Save changes and you’re done

After you have installed the code, you are free to edit it as needed. If you need to include one of the pages sometimes in the future, simply delete its ID number from the code. Same goes for adding more pages to the “excluded basket”. If you, for example, create a new page which you don’t want to show in the search results, feel free to add its ID number to the code.
Allow usage of Voice Search in WordPress

After smartphones became an important part of our lives, voice search has been improving rapidly. Although never perfect, searching something on your smartphones has made things like calling a person a much easier task. Yeah, you still have to be very careful with the pronunciation because you might end up calling someone you don’t want to or your speech to text could end up in something not even close to what you have just spoken. Also, asking Siri silly questions is always fun, right?

It doesn’t matter if you like it or not, many people are using voice search functionality on their phones. So why wouldn’t you allow your visitors to search through your website the same way?

Maybe you didn’t know you can do that with WordPress or you didn’t know how to do it, but now you won’t have an excuse. If you want to learn more about the code behind voice search, you should see the official page of the plugin we’re about to present.

Instead of the code, here’s a plugin which will extend your WordPress search.

Cost: Free

Before you download and install the plugin, it is important to know that it will work only with Chrome and Safari. If you’re OK with the fact that plugin won’t work in any other browser, it will give your visitors ability to search the content using their own voice.

By installing the plugin, you’re not overriding the text input – if a user wants to use the search box in a standard way by typing into it, you should know that functionality is intact.

Also, the plugin should work with any other search plugin you might have installed so you’re free to try it with plugins like Relevanssi or Search Everything.

Go to Plugins->Add New
Search for “Voice Search”
Install and activate the plugin

That’s it. After activating the plugin, you should be able to use voice search without problems. How do you like this feature?

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to create and manage a page in WordPress

Creating pages in WordPressCreating individual pages in WordPress is quite similar to writing a...

Changing your WordPress website location

If you want to change the location of your WordPress website, this tutorial will guide through...

Configuring the basic settings in your WordPress website

This tutorial shows you how to configure the basic settings of your WordPress website. This...

How to create a simple portfolio website with WordPress

Installing WordPress on Todhost is simple and easy. It is part of our free software fould under...

How to control new user registration in WordPress

One problem plaguing WordPress especially membership sites that take user registrations is spam...