get_query_var returns null

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.

1 Answer
1

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;
}

Leave a Comment