I have query var set using the following filter:

add_filter('init', 'add_query_vars');
function add_query_vars() {
    global $wp;

I am able to retrieve the query var using get_query_var('user_login'); in page templates without any hiccup. However, on using get_query_var('user_login'); within a function inside of functions.php, no value is returned.

What is the correct way to retrieve the query var within a function inside of functions.php?

1 Answer 1

function gqv() {
    echo get_query_var('user_login');
add_action('parse_query', 'gqv');
  • Rob Vermeer Thx for the reply. Well, I tried ur solution. Apparently it does not work. The problem that I presume is that get_query_var is not able to retrieve the query var since get_query_var when called in functions.php runs before Wordpress checks and sets the query var. Hence, get_query_var never returns anything when called within functions.php Correct me if I am wrong. Any ideas?
    – John
    Commented Dec 17, 2011 at 17:53
  • Yes indeed this is true. But if you use an action, the function is being fired at the moment when the action is triggered. So maybe you have to try another action for this, depending on when you will need the information and when the information is available. You can find the list of actions here: codex.wordpress.org/Plugin_API/Action_Reference You can try for example add_action('get_header', 'your_function'); This action is being fired when the header is being loaded, so it must have the information about the query vars. But maybe your problem lies somewhere else. Hope this helps. Commented Dec 18, 2011 at 1:53
  • Rob Vermeer Ok this works! I had some other piece of code incorrect. Your solution to retrieve the query_var works perfectly. Thx for the help!
    – John
    Commented Dec 19, 2011 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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