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

Adding direct links to outside pages to search results

…singular name’, ‘textdomain’ ), ‘menu_name’ => _x( ‘Affiliate links’, ‘Admin Menu text‘, ‘textdomain’ ), ‘name_admin_bar’ => _x( ‘Affiliate link’, ‘Add New on Toolbar’, ‘textdomain’ ), ‘add_new’ => __( ‘Add New’, ‘textdomain’ ), ‘add_new_item’ => __( ‘Add New Affiliate link’, ‘textdomain’ ), ‘new_item’ => __( ‘New Affiliate link’, ‘textdomain’ ), ‘edit_item’……=> __( ‘Edit Affiliate link’, ‘textdomain’ ), ‘view_item’ => __( ‘View Affiliate link’, ‘textdomain’ ), ‘all_items’ => __( ‘All Affiliate links’, ‘textdomain’ ), ‘search_items’ => __( ‘Search Affiliate links’, ‘textdomain’ ), ‘parent_item_colon’ => __( ‘Parent Affiliate links:’, ‘textdomain’ ), ‘not_found’ => __( ‘No Affiliate links found.’, ‘textdomain’ ), ‘not_found_in_trash’ =>……__( ‘No Affiliate links found in Trash.’, ‘textdomain’ ), ‘archives’ => _x( ‘Affiliate link archives’, ‘The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4’, ‘textdomain’ ), ‘filter_items_list’ => _x( ‘Filter Affiliate links list’, ‘Screen reader text for the filter links heading on the post…

Indexing image alt texts

Relevanssi by default ignores image alt texts (and other tag attributes). That’s often the right thing to do because alt texts aren’t visible to users. Finding a post with a search term that only appears in an alt text may confuse a user. If your alt texts are well crafted,……they may help search. It’s not hard to make Relevanssi index the alt texts. Add this function to your site: add_filter( ‘relevanssi_post_content’, ‘rlv_index_alt_text’ ); function rlv_index_alt_text( $content ) { $content = preg_replace( ‘/<img .*?alt=”(.*?)”.*?>/is’, ‘ \1 ‘, $content ); return $content; } This function uses the relevanssi_post_content filter hook, which…

WP Download Manager

…these issues. When you upload the files to the Relevanssi attachment reading server, Relevanssi uses the get_attached_file() function to get the file name, but that does not work with WP Download Manager. Thus we need to use the relevanssi_get_attached_file filter hook to provide the file name and path for Relevanssi….…add-on adds an advanced search with the [wpdb_archive_filter] shortcode. That search does not use Relevanssi by default, but it can be modified to make use of Relevanssi with this little function you can add to your site: add_filter( ‘wpdm_packages_query_params’, ‘rlv_use_relevanssi’ ); function rlv_use_relevanssi( $params ) { if ( isset( $params[‘s’]…

Related Posts:

Comment Section:

15 Comments. Leave new

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