0

I have created a page in wordpress to accept query string parameters. For the wordpress to recognize the query string parameters, URL parameter is added to query_vars using the following code snippet in Theme functions (functions.php) file. Below is the code snippet I have added to recognize the URL parameter 'website':

function add_query_vars( $aVars )
{
    $aVars[] = "website";
    return $aVars;
}
// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');

When I access the page using the URL http://example.com/custompage/?website=google, the URL parameter value is blank. Below is the code to access the query string value in page and it doesn't seem to be working.

if(isset($wp_query->query_vars['website'])) {
    $siteName = $wp_query->query_vars['website'];
    echo $siteName;
}

Please let me know what could be wrong in the above approach to access the URL parameter value in the wordpress page.

  • your code works as-is for me – Milo Jan 13 '15 at 4:45
  • 1
    Is $wp_query the object of global WP_Query class? Or custom? – cybmeta Jan 13 '15 at 8:50
1

Global declaration for the variable was missing in the page. After adding the following declaration, code works as expected.

global $wp_query;

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.