How to Redirect to a Post When Search Query Returns a Single Result in WordPress

Home » Snippets » How to Redirect to a Post When Search Query Returns a Single Result in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Have you ever been frustrated when searching your site only to have your single search result buried in pages of irrelevant results? Simplifying the user experience can dramatically improve your website’s engagement and functionality. In this article, we’ll guide you through the process of setting up your WordPress site to automatically redirect to a post when a search query returns a single result. This not only helps to streamline search results, but it also gets your visitors to their destination faster.

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

The core idea of our code snippet is to automatically redirect the user to the single search result if their search query returns only one post. To accomplish this, we define a function named wpturbo_redirect_single_post().

Let’s delve deeper into the function to understand how it works:

function wpturbo_redirect_single_post() {
    if (is_search()) {
        global $wp_query;
        if ($wp_query->post_count == 1) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
            exit;
        }
    }
}

The function kicks off with an if statement which checks if the current page is a search results page. This is done using the WordPress conditional tag is_search(). This built-in function returns true when a site’s search results page is being displayed. This tag ensures that the rest of the code within the function only executes during a search.

Then, the global variable $wp_query is declared, which is used to access the contents of the main WordPress query.

Within the is_search() if statement, another if condition checks whether the post_count of the $wp_query object equals 1. $wp_query->post_count holds the total number of posts being displayed. So, we are essentially identifying situations when our search only returns one result.

In such cases, the function executes wp_redirect(get_permalink($wp_query->posts['0']->ID)) where, the get_permalink() function fetches the URL for the single post available in the search results. Here, $wp_query->posts['0']->ID retrieves the identification ID of the first and only post in the $wp_query post array(from index ‘0’).

Next, wp_redirect() function is used to send the user directly to the URL of the available post. exit; is used after wp_redirect() to make sure that WordPress stops processing the rest of the page once the redirect has commenced.

Lastly, the code snippet concludes with the add_action() function, this hooks the wpturbo_redirect_single_post() function to the template_redirect action.

add_action('template_redirect', 'wpturbo_redirect_single_post');

In layman’s terms, this makes sure that our custom function executes at the correct point during the WordPress loading process. The template_redirect action is triggered just before WordPress determines which template page to load, giving us the perfect opportunity to redirect the user.

Register an account to save your snippets or go Pro to get more features.