Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuckreynolds/5313899 to your computer and use it in GitHub Desktop.
Save chuckreynolds/5313899 to your computer and use it in GitHub Desktop.
Redirect to post if a search only returns one post.. Just paste the following code snippet into your functions.php file:
add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
if (is_search()) {
global $wp_query;
if ($wp_query->post_count == 1) {
wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment