Skip to content

How To Auto-Redirect When WordPress Search Only Returns One Post

This is something that I’m currently using on this website, and a snippet which I really like. Here’s a snippet which will automatically redirect your users when the search only returns one post in your WordPress.

Just add the following snippet to your current theme’s functions.php file:

add_action('template_redirect', 'one_match_redirect');
function one_match_redirect() {
    if (is_search()) {
        global $wp_query;
        if ($wp_query->post_count == 1) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );

Leave a Reply

Your email address will not be published. Required fields are marked *