Search result is single than redirect to wordpress post

Users always use the search the keywords in wordpress sites. Many times clients wish about this functionality. If there is only one result came from search result than user should redirect to single landing post.

Search result is single than redirect to wordpress post

If there is Search result is single then redirect to wordpress should redirect to single landing post.

You should open the functions.php file and  put following code in functions.php file.

<pre>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 ) );
        }
    }
}
Search result is single than redirect to wordpress post

Search result is single than redirect to wordpress post

Add a Comment

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