If the search results are only in one post, then how to redirect in that post?

1610

How are you all? I hope all of you are fine. Today I will discuss with which subject, that is search results are only in one post, then how to redirect in that post?

How to Redirect to post when a search query returns a single result?

wordpress2

When any visitor search anything in own search box of WordPress then search results are shown in list form. If search’s results are only in one post, but in that post for redirecting, you can improve your search engine.

Previous Article: How to upgrade WordPress comparatively in slow server?

For improving search engine, paste following code in function file (functions.php) of your theme. Save that file. Now if search engine gets only one post of searching, it will redirect in that post.

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

Those who don’t read my previous post, for them previous post of WordPress link is given in the following.

What’s your Reaction?
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0