Pasting and copying the following snippet in your theme’s functions.php file will automatically redirect your website visitors to the first post if only one post is found.
1 2 3 4 5 6 7 8 9 | 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 ) ); } } } |
Snippet Source/Credit: Paulund.co.uk