Go Straight to a Single Search Result
When a search returns exactly one result, opens that result directly instead of showing a results page.
Opens WPIDE on
Code
add_action( 'template_redirect', function () {
global $wp_query;
if ( is_search() && ! is_paged() && 1 === (int) $wp_query->found_posts && ! empty( $wp_query->posts ) ) {
wp_safe_redirect( get_permalink( $wp_query->posts[0] ) );
exit;
}
} );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.