Created
July 24, 2022 07:02
-
-
Save Yorlinq/6fdca253c5eb5e0dd16176bb2d236a31 to your computer and use it in GitHub Desktop.
Redirect search to post with only 1 search result - WordPress
This file contains hidden or 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 search to post with only 1 search result | |
add_action('template_redirect', 'yl_auto_redirect_one_search_match'); | |
function yl_auto_redirect_one_search_match() { | |
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