1

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.

5
  • 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, 2012 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, 2012 at 15:56
  • I guess I'm suppose to register it, huh?
    – AlxVallejo
    Sep 27, 2012 at 15:58
  • yes, see my answer.
    – Milo
    Sep 27, 2012 at 15:59
  • ps. not 'elaborating' but the opposite of that :)
    – AlxVallejo
    Sep 27, 2012 at 16:10

1 Answer 1

6

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;
}
3
  • Would it be more efficient to run global $wp; $wp >add_query_var('tree'); ?
    – AlxVallejo
    Sep 27, 2012 at 16:03
  • no difference in efficiency, but the filter is the proper way to add them
    – Milo
    Sep 27, 2012 at 17:33
  • Note: I had the same issue, add_filter fixed it but I also had to declare my $page variable below get_header()
    – hellojebus
    Feb 7, 2017 at 0:00

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.