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.

My pretty permalink clearly shows the query_var:

localhost/site/?tree=312

Yet when I run

var_dump(get_query_var('tree'));

I get NULL returned.

Any reason why? Also when I print_r($wp_query), I can't find 'tree' anywhere.

share|improve this question
    
well first, that isn't a pretty permalink, it's a query string argument. what is tree? is it a registered query var? –  Milo Sep 27 '12 at 15:54
    
@Milo - I'm elaborating on the url. It's more like localhost/site/category/?tree=312. I added the query_arg when I displayed the link: add_query_arg('tree',$post->ID,get_permalink($post->ID)); I'm basically passing the ID in the URL and I thought I'd give it a name of 'tree'. –  AlxVallejo Sep 27 '12 at 15:56
    
I guess I'm suppose to register it, huh? –  AlxVallejo Sep 27 '12 at 15:58
    
yes, see my answer. –  Milo Sep 27 '12 at 15:59
    
ps. not 'elaborating' but the opposite of that :) –  AlxVallejo Sep 27 '12 at 16:10

1 Answer 1

up vote 1 down vote accepted

You have to add any query vars that are not WordPress objects to the array of recognized query vars to be able to retrieve it from the $wp_query global:

add_filter( 'query_vars', 'wpa66452_query_vars' );
function wpa66452_query_vars( $query_vars ){
    $query_vars[] = 'tree';
    return $query_vars;
}
share|improve this answer
    
Would it be more efficient to run global $wp; $wp >add_query_var('tree'); ? –  AlxVallejo Sep 27 '12 at 16:03
    
no difference in efficiency, but the filter is the proper way to add them –  Milo Sep 27 '12 at 17:33

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.