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?
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.
- How to show Author’s Posts Only in Author Dashboard in WordPress?,
- How to remove the URL field from the WordPress tuments form?,
- How to migrate a WordPress site from a host to another host with the plugin?,
- How to download installed WordPress theme from dashboard?
- How to create a music library with WordPress?.