Search result is one then land or redirect to single wordpress post

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

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.