Steps
-
Add the PHP code to your theme’s
functions.phpfile or a PHP snippets plugin. -
Save and activate the snippet.
-
Search for a keyword that matches exactly one post.
-
WordPress will redirect directly to that post.
/**
* Redirect search queries with one result
*/
function custom_redirect_single_search_result() {
if ( is_admin() || ! is_search() ) {
return;
}
global $wp_query;
if ( $wp_query->post_count === 1 ) {
$post = $wp_query->posts[0];
wp_safe_redirect( get_permalink( $post->ID ), 301 );
exit;
}
}
add_action( 'template_redirect', 'custom_redirect_single_search_result' );
Tested on
WordPress 6.8
Reported by the author on November 16, 2026. WPFolks has not verified this.
Not tested against WordPress 7.1.5. It may still work.
