WordPress Tutorials

How to Auto-Redirect When WordPress Search Query Only Returns One Match

Sometimes is the little things that make a big difference. Saving your visitors from a click here and a scroll there can make the user experience just a little more enjoyable. This can further help in boosting the conversion rates as well.

If you provide your visitors with the ability to search your website, there will be times when the results returned might be just a single page or post. In those situations, it can be helpful to direct visitors to the individual post instead of to a search results page where they must then click through to a single result.


In this WordPress tutorial, we’ll show you how to set up your website to perform an auto-redirect any time a user search query returns only one potential match without editing the .htaccess file.

How to Auto-Redirect When WordPress Search Query Only Returns One Match

This is a relatively easy improvement to make, but there are some things you should be aware of before you get started. The first issue is that your functions.php file is a critical file. If you make an error when editing this file, you could easily break your website–not good. Whenever possible you should avoid making changes directly to your functions.php file.

There are a few potential solutions to managing this challenge:

  1. Use a child theme and make any changes necessary to the child theme files.
  2. Use a WordPress plugin like Code Snippets which is available for free in the WordPress plugin repository.
  3. Create a plugin.

Ok, now that you have a better understanding of the importance of managing your custom functions properly. Let’s take a look at how you can make the required changes to your search functionality. We’re going to assume that we’re editing a child theme for this example. Here are the steps:

1 Login to your cPanel, and open up your file manager.

Login to Cpanel, open File Manager and Navigate to /wp-content/themes/child-theme-name/ Folder.

2 Create a Backup of Functions.php.

Always create a backup file before editing

Inside your child theme folder, select the functions.php file and click on copy. When the popup opens, under “Copy file to:” add the following: /functions-backup.php – this file will now serve as your backup in case something goes wrong with your original file. If that were the case, you could simple delete your broken file and rename the copy to just functions.php.

3 Edit Functions.php.

Next, select the functions.php file and click on Edit (click edit a second time when the popup box appears).
Scroll to the bottom of your file and paste in the following code as seen in the picture below:

add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
  if (is_search()) {
    global $wp_query;
    if ($wp_query->post_count == 1) {
      wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );

4 Paste the code.

Paste the code above into the bottom of the functions.php file and save

Click Save in the top right corner and close the window.

Once you’re done that, head to your site and test out the search function. If you enter an obscure search term for which there is only one result, you should now find that you are redirected to the actual post instead of a search result. That’s it!

Download 3 WordPress themes & plugins​ with lifetime access for FREE!