Skip to content

Instantly share code, notes, and snippets.

@iscat
Created May 18, 2013 14:14
Show Gist options
  • Save iscat/5604548 to your computer and use it in GitHub Desktop.
Save iscat/5604548 to your computer and use it in GitHub Desktop.
<?php
/* Redirect to post if search results only returns one post.
http://blog.wpjam.com/m/redirect-to-post-if-search-results-only-returns-one-post/
------------------------------------------------------------------------------ */
add_action('template_redirect', 'search_redirect_post');
function search_redirect_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