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 have query var set using the following filter:

add_filter('init', 'add_query_vars');
function add_query_vars() {
    global $wp;
    $wp->add_query_var('user_login');
}

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted
function gqv() {
    echo get_query_var('user_login');
}
add_action('parse_query', 'gqv');
share|improve this answer
    
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 Dec 17 '11 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. –  Rob Vermeer Dec 18 '11 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 Dec 19 '11 at 3:56

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.