Skip to main contentSkip to footer

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

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Riode
Riode is a WooCommerce theme from ThemeForest. It has a built-in live search that does not automatically use Relevanssi. However,…
Yoast SEO
Starting from versions 2.4.0 (Premium) and 4.3.0 (Free), Relevanssi doesn’t index posts that have been marked “noindex” in the Yoast…

Related Posts:

Comment Section:

17 Comments. Leave new

  • Fun fact:
    If you try to add the first snippet to a site that has a typeahead live AJAX search, it might break it — redirecting the typeahead results to the single result page as you type. This was tested with an (old) custom theme that was running its own AJAX search system, so I have no idea if this is broken using the official plugin.

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

    Reply
  • 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?!

    Reply
  • 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,

    Reply
  • 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?

    Reply
  • 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?

    Reply
    • 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?

      Reply

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed