I have query vars defined in functions.php using:
add_filter('init', 'add_query_vars');
function add_query_vars() {
global $wp;
$wp->add_query_var('profession');
}
Until now, to retrieve the query var in page templates, I have been using $profession = get_query_var('profession');
. A similar example $paged = get_query_var('paged');
is also shown for the $paged
variable on Codex page for get_query_var()
. However, today I learnt that the query variables are accessible as global variables. All I have to do is declare global $profession;
and I can start using the value of the variable anywhere within the template. This to me appears to be a much better way of accessing the query vars without the overheads of nested functions of get_query_var()
.
Is there a good reason to use get_query_var()
to fetch the query vars instead of accessing them as global variables? If not, then why does get_query_var()
even exists?