Skip to main contentSkip to footer

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

WP e-Commerce
WP e-Commerce is a popular shopping cart plugin for WordPress. This code by Alexander Gieg, added to functions.php for example,…
Relevanssi Premium as a Composer package

…use the following: https://www.relevanssi.com/update/get_version.php?api_key=XXX&version=2.20.1 The Composer package should look something like this: { “type”: “package”, “package”: { “name”: “relevanssi/relevanssi-premium”, “version”: “2.20.1”, “type”: “wordpress-plugin”, “dist”: { “type”: “zip”, “url”: “https://www.relevanssi.com/update/get_version.php?api_key=XXX&version=2.20.1″ }, } } WP CLI also supports installing plugins from URLs. You can do this to install Relevanssi Premium: wp plugin…If you want to install Relevanssi Premium as a Composer package, you can use the following URL to fetch the latest version: https://www.relevanssi.com/update/fetch_latest_version.php?api_key=XXX Replace XXX with your valid API key. The URL will always return the latest version of the plugin files. If you want a specific version, you can……install https://www.relevanssi.com/update/fetch_latest_version.php?api_key=XXX Both URLs support HTTP basic auth. If you don’t include the api_key parameter, you’ll be prompted for your user name and password. Use your Relevanssi.com account user name (not the login email) as the user name and your API key as the password. These can be stored in…

Pinning posts
Sometimes you want to get a specific post as the first result for a certain search, but Relevanssi doesn’t return…

Related Posts:

Comment Section:

15 Comments. Leave new

Leave a Reply to Mikko Saari Cancel 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