Posted By

adambundy on 05/28/15


Tagged

php wordpress


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

adambundy


WordPress - redirect to the post instead of archive page when search query returns single result


 / Published in: PHP
 

URL: http://wpsnipp.com/index.php/functions-php/redirect-to-post-when-search-query-returns-single-result/

Courtesy Kevin Chard at WPSnipp.com.

Adding this snippet to the functions.php of your wordpress theme will redirect your search to the post automatically when wordpress only returns a single search result.

  1. add_action('template_redirect', 'single_result');
  2. function single_result() {
  3. if (is_search()) {
  4. global $wp_query;
  5. if ($wp_query->post_count == 1) {
  6. wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
  7. }
  8. }
  9. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Jacky888 on August 9, 2018

root itel a11 bestandroidtoroot

You need to login to post a comment.