Skip to content

Instantly share code, notes, and snippets.

@sheabunge
Created June 3, 2013 00:15
Show Gist options
  • Save sheabunge/5695462 to your computer and use it in GitHub Desktop.
Save sheabunge/5695462 to your computer and use it in GitHub Desktop.
If the search query only returns a single post, redirect to that post.
<?php
/**
* If the search query only returns a single post, redirect to that post.
*
* @return void
* @author Paulund
* @link http://www.paulund.co.uk/redirect-search-results-return-one-post
*/
function redirect_single_post() {
if ( ! is_search() )
return;
global $wp_query;
if ( $wp_query->post_count == 1 && $wp_query->max_num_pages == 1 ) {
wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
exit;
}
}
add_action('template_redirect', 'redirect_single_post');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment