Go straight to the only search result
When a search finds exactly one post, the visitor goes straight to it instead of a results page with one link.
<?php
add_action(
'template_redirect',
static function () {
global $wp_query;
if ( is_search() && ! is_paged() && 1 === (int) $wp_query->found_posts && ! empty( $wp_query->posts[0] ) ) {
wp_safe_redirect( get_permalink( $wp_query->posts[0] ) );
exit;
}
}
);
In these packs
- Search and archives: Better site search and cleaner archive pages.