How To Redirect to that WordPress Post, if Search Results Shows Only that Post.

952

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



How To Redirect to that WordPress Post, if Search Results Shows Only that Post.



How-To-Redirect-to-that-WordPress-Post,-if-Search-Results-Shows-Only-that-Post
How-To-Redirect-to-that-WordPress-Post,-if-Search-Results-Shows-Only-that-Post

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

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 tune of searching, it will redirect in that tune.

Code Given Below.

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

}

}

}

 

If you like this article or facing any issue then don’t forget to comment below.

Thank You…!