Created
October 16, 2016 17:50
-
-
Save mahdiyazdani/e00d7fb4d4d4332d5e142831d8df964d to your computer and use it in GitHub Desktop.
Redirect To Post If Search Results Return One Post
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
| <?php | |
| function my_redirect_single_post() { | |
| if (is_search()) { | |
| 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', 'my_redirect_single_post'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment