X

How to Redirect When WordPress Search Only Returns One Match

Snippets by IsItWP

Do you want to redirect your search to the post if there’s only one search result? This snippet improves the onsite search experience by eliminating the need for a click.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

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

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: Improve the Default WordPress Search Experience – the Complete Search WP Review.

Comments   Leave a Reply

  1. I get a syntax error, unexpected ‘$wp_query’ (T_VARIABLE) when i copy paste in your code in the functions.php file. So what i have is a custom post type named cars with 4 taxonomies and i have made an archive page that uses the grid plugin and the search and filter pro plug-in. All works well but i want to have a redirected to the post page instead if i have only one result in the archive. Can you give some hints ? Thanks !

  2. Thanks!

  3. 12 Not-So-Obvious WordPress Tweaks to Improve Posts and Pages - WPMU DEV March 27, 2015 at 8:10 am

    […] really like this one – thanks WP Snipp! When a user searches your website and there is only one result the user is taken straight to the […]

  4. WordPress Conditional Tags (And Snippets) for Beginners March 4, 2015 at 11:52 pm

    […] [Source] […]

  5. Cameron Coleman July 26, 2013 at 6:31 pm

    Nice code but I would make one improvement… change

    if ($wp_query->post_count == 1) to
    if ($wp_query->found_posts == 1)
    The original code breaks pagination when there is only one post left on the next page and redirects to it instead of showing it. eg. If a post archive is set to show 10 post per page and 11 results are returned then instead of showing the last post it redirects to it.

    1. this is working properly, thanks!

      1. Glad it helped 🙂

  6. 29 Wordpress Tweaks to Improve Posts and Pages October 18, 2011 at 9:03 am

    […] [Source: WPSNIPP] […]

  7. Your sample worked fine for me, everything appears to work working.

    1. Cliff Paulick July 25, 2011 at 6:37 pm

      Hey, me too now. Sorry. Thanks.

      1. no problem 🙂

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!