Posted on

Auto-redirecting with one search result

If you want to auto-direct the user directly to the result if there’s just one result for the search, that’s quite easy. Just add the following code to your theme functions.php:

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 ) );
            exit();
        }
    }
}

This is not Relevanssi-specific and works just fine even without Relevanssi. If you are using Relevanssi on a multisite system, you need a small change to the code:

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

The code is originally from WP Beginner.

If you want to redirect particular search terms to posts without going through the search results page, that looks like this:

add_action('template_redirect', 'search_term_redirect');
function search_term_redirect() {
    if (is_search()) {
        global $wp_query;
        if ($wp_query->query_vars['s'] == 'term') {
            wp_redirect( 'http://www.example.com/target-page/' );
            exit();
        }
        if ($wp_query->query_vars['s'] == 'word') {
            wp_redirect( 'http://www.example.com/another-target-page/' );
            exit();
        }
    }
}

12 comments Auto-redirecting with one search result

  1. Hi! This functionality is exactly what I need. However, i’m looking to re-direct based on a custom field (sku), and it doesn’t seem to work. Do I need to replace post_count and posts with product, etc?

    1. No, no changes should be necessary, because this code doesn’t do any matching, this just redirects based on Relevanssi search results. Do you find posts by SKU otherwise? Have you set Relevanssi to index the SKUs?

          1. Figured it out! I just had to put the code above other add_actions in the functions file. Works great now.

  2. Is there a way to maintain the search term highlighting that would have been present if the single page/post was loaded from the search results list?

  3. Hi, the plugin find the search word well, but it is not linked to the page… it shows only the sentence where the word is… I’m doing something wrong ?
    Thanks for your support.
    erem,

  4. Thank you guys for this!

    i am using the last option “edirect particular search terms to posts without going through the search results”.
    Unfortunately right now the script CARES about capitalization or not.
    So if you would search for “Term” or “terM” you would NOT be redirected to http://www.example.com/target-page/ !

    Any idea how to modify the code?!

Leave a Reply

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