DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

When a reader search your site using WordPress built-in search engine, the results are displayed as a list. Here is a snippet to improve the search engine by automatically redirecting the reader to the post if only one post is found by WordPress search engine.

Just paste the following code snippet into your functions.php file:

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

Thanks to Paulund.co.uk for the snippet!



  1. Don’t forget to exit; after redirect, and account for multiple pages. If you search query has 11 posts, and you only display 10 per page, then clicking ‘next page’ will cause a redirection to the last post instead of displaying it as the 2nd page.


  2. Well the above script checks to see if a category has a post however you could simply check if it is an archive and then redirect to homepage.

    function redirect_to_home(){
    if( is_archive()){
    } add_action(‘template_redirect’, ‘redirect_to_home’);

  3. That’s a nice snippet. Certainly good for SEO (no frustrating duplicate content) and potentially good for users. I think I’d want to display a message on the post page to inform the user that they were redirected here because this was the only result, how would you suggest doing that?

  4. I have tried so many ways to get my searches to redirect to a specific page with no success. I don’t know what I’m doing and could really use help. My clients site is http://privilegedpets.com/ I have 2 pages with a list of unique zipcodes on each page. I’m trying to redirect visitors to those pages where the zipcodes are located. But when I embed your rediect code nothing happens from the search page located here – http://privilegedpets.com/locate-a-sitter

    However, if I remove the code i get the results which only show the one unique page. Please guide me on this. Thanks. For temporary help user name is wpbeg and password is wpbeg1234 for access to my site. You will see the code snippet at bottom of functions page. It might be a different form in function name since I found similar snippets elsewhere.

    Thanks to anyone who helps me.

  5. Thanks, that’s what I was looking $wp_query->post_count with this is solved my problem.

Leave a Comment

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