How to redirect search result page to post

I think it is better that you do this inside the WordPress install, not on the .htaccess. Is not easy to maintain and maybe a problem, if you have more results for a search. You can use the follow source, write in a small plugin and activate. If the search result have only one result, then will rewrite to the single post.

add_action( 'template_redirect', 'fb_single_result' );
function fb_single_result() {

    if (is_search()) {
        global $wp_query;

        if ( 1 === $wp_query->post_count ) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
            exit();
        }
    }
}

Leave a Comment