Skip to content

Instantly share code, notes, and snippets.

@miklb
Created April 21, 2012 13:28
Show Gist options
  • Save miklb/2437076 to your computer and use it in GitHub Desktop.
Save miklb/2437076 to your computer and use it in GitHub Desktop.
WordPress redirect single search result
/*If only one search result match, redirect to that page */
function kkf_singlesearch_redirect() {
if (is_search()) {
global $wp_query;
if ($wp_query->post_count == 1) {
wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
}
}
}
add_action('template_redirect', 'kkf_singlesearch_redirect');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment