Display title for search engine visitors

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;.

Read More

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=

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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'] ) );