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(); } } }
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?
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?
Hmm… not sure what’s going on then. Yep, I set up Relevanssi to search all custom fields (including SKU). If you type in 0012 into the search here: http://dev.andriannashamarisinc.com/ … it still just displays a single result without re-directing.
I’d do some debugging on the redirection code. Does it run? Does it notice there’s just one result? Is something blocking the redirection?
Figured it out! I just had to put the code above other add_actions in the functions file. Works great now.
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?
Probably not, I think the redirect drops out the necessary info about the search query that Relevanssi would need.
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,
Sounds like a problem with your search results template.
Thanks for your answer ! You’re right… I modifies thé CSS.
Thanks again for your support !
R
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?!
if (strtolower($wp_query->query_vars[‘s’]) == ‘term’)…
Hello! Can the first auto-redirect snippet be modified to work with click tracking? Currently, it doesn’t drop the rt_nonce URL parameter.
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()
.Awesome, thank you!