0

As we know that when we search wordpress based site it shows this type URL on browser

__http://www.wordpressdemo.com/?s=wordpress

now at this moment our query is wordpress

for demo please view below link

http://www.opensourcecms.com/scripts/search.php

its first result is

http://www.opensourcecms.com/scripts/details.php?scriptid=88&name=WordPress

my requirement is when user search a query wordpress automatic show 1st result post Instead of result page.

  • It's unclear to me what you're asking here and what these links are? Please clarify, thanks. – birgire Dec 25 '15 at 17:39
  • when user search a query wordpress automatic show 1st result post Instead of result page.well @s_ha_dum answer – Bilal Raj Dec 25 '15 at 18:19
1

Redirecting to the first result of a search is a very unfriendly thing to do to your visitors and can quite possibly prevent them from ever finding the content they need, but it is very easy to do:

function redir_first_wpse_49208() {
  if (is_search()) {
    global $wp_query;
    if (!empty($wp_query->posts)) {
      wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
      exit;
    }
  }
}
add_action('template_redirect', 'redir_first_wpse_49208');
  • thank you very much and suggestion but i am using this where query have only one result. – Bilal Raj Dec 25 '15 at 18:19
  • I don't see how you can guarantee with any certainty that an ?s= search will only return a single result. – s_ha_dum Dec 25 '15 at 18:24
  • hmm....probability btw is it possible i can pass query as title to end result page – Bilal Raj Dec 25 '15 at 18:27
  • @birgire why you remove 'exit;' from above code ? – Bilal Raj Dec 25 '15 at 18:41
  • I'm adding the exit part not removing it. We need it after the wp_redirect() call. @BilalRaj – birgire Dec 25 '15 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.