wordpress snippets last updated   : January 29, 2015
we now have 646 snippets

Redirect to post when search query returns single result

wordpress snippet

Adding this snippet to the functions.php of your wordpress theme will redirect your search to the post automatically when wordpress only returns a single search result.

snippet :  PHPcopy
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 ) );
        }
    }
}

Did you like this post? If so please share it with your friends. Thanks!