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. Add the following code to your site:

add_action( 'template_redirect', 'one_match_redirect' );
function one_match_redirect() {
    if ( is_search() ) {
        global $wp_query;
        if ( 1 === $wp_query->post_count ) {
            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 ( 1 === $wp_query->post_count ) {
            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 ( 'term' === $wp_query->query_vars['s'] ) {
            wp_redirect( 'http://www.example.com/target-page/' );
            exit();
        }
        if ( 'word' === $wp_query->query_vars['s'] ) {
            wp_redirect( 'http://www.example.com/another-target-page/' );
            exit();
        }
    }
}

15 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?!

  5. Hello! Can the first auto-redirect snippet be modified to work with click tracking? Currently, it doesn’t drop the rt_nonce URL parameter.

    1. Sure. The easiest way is to disable the click tracking temporarily with the following:

      add_filter( 'pre_option_relevanssi_click_tracking', 'relevanssi_return_off' );

      Add this line just before the wp_redirect().

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

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