Skip to content

How To Redirect To Post If Search Results Return One Post In WordPress

WordPress’s built-in search function allow you and your visitors to easily search posts on your WordPress site by typing the keyword. Here’s a trick which will redirect to post if search results return one post. Why waste user’s time if his term only matches in the one post in your blog. This trick will directly take him to the post, instead of showing the search page with only a single result.

Add following snippet to your current theme’s functions.php file to check if the search result only has one post, if it has then it performs a redirection to the post:

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

Leave a Reply

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