Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I read somewhere*) that there's some q part inside the wp_query object, that identifies search engine visitors and shows the keywords they searched for.

I'm currently developing local, so i can't access/see $GLOBALS['wp_query']->q;.

Could someone varify if this exists and/or has some alternate solution for retrieving:

  • the search engine name
  • the search string searched inside the engine?

*) Can't remember where, but it had something to do with "better search results page" & "hightlighting search terms".

-- EDIT --

This is what i got so far:

// declare the server itself as invalid to only catch search engine results
$http_referer   = explode( "/", $_SERVER['HTTP_REFERER'] );
$http_referer   = $http_referer[2];
$home_url       = explode( "/", home_url() );
$home_url       = $home_url[2];

// Coming from a search engine: modify to a search string
if ( isset( $_SERVER['HTTP_REFERER'] ) && $home_url !== $http_referer )
{
    // remove previous "s" search query args
    remove_query_arg( 's' );

    $string = parse_url( $_SERVER['HTTP_REFERER'] );
    $query = $string['query'];
    $query = explode( "&", $query );
    foreach ( $query as $q )
    {
        $q = urldecode( $q );
        $arr = explode( "=", $q );
        if ( (string) 'q' === $arr[0] )
            $search_string = $arr[1];
    }

    // build a new "s" search query arg: if isset, from the string entered in the search engines input form
    // in theory that should allow treating it like a normal search result
    // the draw back is, that a search engine visitor would never get directly to a post or page 
    if ( isset( $search_string ) )
        add_query_arg( array( 's' => $search_string ) );
}

Aside from the drawback that no search engine visitor would come to a result directly, i wouldn't catch all search engines that way. Google & Altavista got a q=searchstring query argument. Yahoo for eg. uses p=...

share|improve this question
    
you could do a check for a few known hosts: $isgoogle = strpos($string['host'],'google'); then grab the appropriate string for that host. –  Milo May 8 '11 at 14:40

2 Answers 2

I just ran a test on my hosted dev site. I ran the following:

echo '<pre>';
global $wpdb;
print_r($wpdb);
print_r($GLOBALS);
echo '</pre>';

There was no 'wp_query', '[q]', or 'search' variables to be found. Note that I was not able to search this through a search engine as it's not web accessible.

Just to give you a little more, here is the whole object I get: http://pastebin.com/qV2QSHYf.

share|improve this answer
    
Wow, mate! That's nice even if it didn't bring anything up. +1 –  kaiser May 4 '11 at 4:26

I've never heard this of wp_query, but could you look at $_SERVER['HTTP_REFERER'] to get this? I've never tried personally.

if( isset($_SERVER['HTTP_REFERER']) )
    print_r( parse_url( $_SERVER['HTTP_REFERER'] ) );
share|improve this answer
    
I just ran a test on google and got this result, so i guess it should be that way http://www.google.at/search?q=test+me+now –  kaiser May 4 '11 at 4:45
    
you can use php's parse_url() to get the host and query. edited my answer to add some code. –  Milo May 4 '11 at 4:56
    
the problem is, that i'd have to decide what $_SERVER['HTTP_REFERER'] is a valid external referer - or somehow deny localhost and/or the server url. When print_r-ing the http referer i also get for ex. http://localhost/wordpress/?s=test. So far i could filter out the search string from a google search, but i'm really not sure about how to get the search string from any engine displayed as search result. I'll update your answer to show you the "so far how-to" (which is not satisfying). –  kaiser May 4 '11 at 18:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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