/ Published in: PHP

Courtesy Kevin Chard at WPSnipp.com.
Adding this snippet to the functions.php of your wordpress theme will redirect your search to the post automatically when wordpress only returns a single search result.
Adding this snippet to the functions.php of your wordpress theme will redirect your search to the post automatically when wordpress only returns a single search result.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
add_action('template_redirect', 'single_result'); function single_result() { if (is_search()) { global $wp_query; if ($wp_query->post_count == 1) { wp_redirect( get_permalink( $wp_query->posts['0']->ID ) ); } } }
URL: http://wpsnipp.com/index.php/functions-php/redirect-to-post-when-search-query-returns-single-result/
Comments
