get_query_var function not working at all

WordPress doesn’t automatically add all query string params ($_GET params) as query_vars.

When Query Var somevar is not registered:
example.com/some-page/?somevar=hello
– WordPress ignores somevar

When Query Var somevar is registered:
example.com/some-page/?somevar=hello
– WordPress stores the value of this param in the $wp_query->query_vars array

The difference between registering that variable with WordPress is whether the value is stored in the WP_Query object.. (it should still be available via $_GET regardless).

To register your custom query var, you should use:

add_filter('query_vars', 'add_my_var');
function add_my_var($public_query_vars) {
    $public_query_vars[] = 'some_unique_identifier_for_your_var';
    return $public_query_vars;
}

Also look out not to use default WordPress query_var names – you’ll probably get some conflicts then, I guess.

Leave a Comment