Quantcast
Channel: WPRecipes
Viewing all articles
Browse latest Browse all 121

How to redirect to post if search results only returns one post

$
0
0

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!


Viewing all articles
Browse latest Browse all 121

Trending Articles