Sets the value of a query variable in the WP_Query class.
Parameters
$query_var
stringrequired- Query variable key.
$value
mixedrequired- Query variable value.
Source
function set_query_var( $query_var, $value ) {
global $wp_query;
$wp_query->set( $query_var, $value );
}
Changelog
Version | Description |
---|---|
2.2.0 | Introduced. |
One use case for this is to pass variable to template file when calling it with
get_template_part()
.Now in you template you can then call it.
This is a way to pass variables to the called files.
On the a.php file:
On the b.php file:
On the c.php file:
extract()
is regarded as a “terrible function” by WordPress coding standards; please do not use.list()
is similar toextract()
, and is much more readable.To update examples and avoid spread of
set_query_var()
in recents projects.Lot of them continue to use
set_query_var()
for passing args/vars to a template withget_template_part()
.Since WordPress 5.5 (08/11/2020), think to use the $args third parameter to passed args to a template or a partial.
Doc about passing arguments to template files :
https://make.wordpress.org/core/2020/07/17/passing-arguments-to-template-files-in-wordpress-5-5/