Created
December 7, 2017 12:49
-
-
Save everaldomatias/d1f51220de04f41fd5512555b9e976c7 to your computer and use it in GitHub Desktop.
Snippet to redirect single result search in WP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Redirect single result search in WP. | |
* @author Everaldo Matias <[email protected]> | |
*/ | |
add_action( 'template_redirect', 'redirect_single_result_search' ); | |
function redirect_single_result_search() { | |
if ( is_search() ) { | |
global $wp_query; | |
if ( $wp_query->post_count == 1 ) { | |
wp_redirect( get_permalink( $wp_query->posts['0']->ID ) ); | |
exit; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment