Title: set_query_var
Published: April 25, 2014
Last modified: February 24, 2026

---

# set_query_var( string $query_var, mixed $value )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/set_query_var/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/functions/set_query_var/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/set_query_var/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/set_query_var/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/set_query_var/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/set_query_var/?output_format=md#wp--skip-link--target)

Sets the value of a query variable in the [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/)
class.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/set_query_var/?output_format=md#parameters)󠁿

 `$query_var`stringrequired

Query variable key.

`$value`mixedrequired

Query variable value.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/set_query_var/?output_format=md#source)󠁿

    ```php
    function set_query_var( $query_var, $value ) {
    	global $wp_query;
    	$wp_query->set( $query_var, $value );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/query.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/query.php#L74)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/query.php#L74-L77)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/set_query_var/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Query::set()](https://developer.wordpress.org/reference/classes/wp_query/set/)`wp-includes/class-wp-query.php` |

Sets the value of a query variable.

  |

| Used by | Description | 
| [wp_list_comments()](https://developer.wordpress.org/reference/functions/wp_list_comments/)`wp-includes/comment-template.php` |

Displays a list of comments.

  | 
| [comments_template()](https://developer.wordpress.org/reference/functions/comments_template/)`wp-includes/comment-template.php` |

Loads the comment template specified in $file.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/set_query_var/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.2.0](https://developer.wordpress.org/reference/since/2.2.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/set_query_var/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/set_query_var/?output_format=md#comment-content-2285)
 2.    [stode](https://profiles.wordpress.org/stode/)  [  9 years ago  ](https://developer.wordpress.org/reference/functions/set_query_var/#comment-2285)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_query_var%2F%23comment-2285)
     Vote results for this note: 11[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_query_var%2F%23comment-2285)
 4.  One use case for this is to pass variable to template file when calling it with`
     get_template_part()`.
 5.      ```php
         // When calling a template with get_template_part()
         set_query_var('my_form_id', 23);
         get_template_part('my-form-template');
         ```
     
 6.  Now in you template you can then call it.
 7.      ```php
         // Inside my-form-template.php
         $my_form_id = get_query_var('my_form_id');
         ```
     
 8.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_query_var%2F%3Freplytocom%3D2285%23feedback-editor-2285)
 9.   [Skip to note 5 content](https://developer.wordpress.org/reference/functions/set_query_var/?output_format=md#comment-content-3270)
 10.   Anonymous User  [  7 years ago  ](https://developer.wordpress.org/reference/functions/set_query_var/#comment-3270)
 11. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_query_var%2F%23comment-3270)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_query_var%2F%23comment-3270)
 12. This is a way to pass variables to the called files.
 13. On the a.php file:
 14.     ```php
         $sample = 'a sample variable';
         $year = 2019;
     
         $arr = [
         	'sample' =&gt; $sample,
         	'year' =&gt; $year
         ];
     
         set_query_var( 'multiVar', $arr );
         get_template_part( 'b' );
         get_template_part( 'c' );
         ```
     
 15. On the b.php file:
 16.     ```php
         $arr = get_query_var( 'multiVar' );
         echo $arr['year']; // This will print out: 1995
         ```
     
 17. On the c.php file:
 18.     ```php
         $arr = get_query_var( 'multiVar' );
         echo $arr['sample']; // This will print out: a sample variable
         ```
     
 19.  * IMO: Using extract method can help readability, in my case allowed me not rename
        variables already in use. On the c.php file:
      *     ```php
            extract( get_query_var( 'multiVar' ) );  echo $sample; // This will print out: a sample variable 
            ```
        
      * [Ivo Vicente](https://profiles.wordpress.org/ivolvicente/) [7 years ago](https://developer.wordpress.org/reference/functions/set_query_var/#comment-3350)
      * `extract()` is regarded as a “terrible function” by [WordPress coding standards](https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#dont-extract);
        please do not use. [`list()`](https://www.php.net/manual/en/function.list.php)
        is similar to `extract()`, and is much more readable.
      * [crstauf](https://profiles.wordpress.org/crstauf/) [6 years ago](https://developer.wordpress.org/reference/functions/set_query_var/#comment-3810)
 20.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_query_var%2F%3Freplytocom%3D3270%23feedback-editor-3270)
 21.  [Skip to note 6 content](https://developer.wordpress.org/reference/functions/set_query_var/?output_format=md#comment-content-6760)
 22.   [jnlr](https://profiles.wordpress.org/jonathanlo/)  [  2 years ago  ](https://developer.wordpress.org/reference/functions/set_query_var/#comment-6760)
 23. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_query_var%2F%23comment-6760)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_query_var%2F%23comment-6760)
 24. 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
     with `get_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.
 25.     ```php
         // No example here as this is not the appropriate page
         But for example with get_template_part : https://developer.wordpress.org/reference/functions/get_template_part/#comment-4130
         ```
     
 26. Doc about passing arguments to template files :
      [https://make.wordpress.org/core/2020/07/17/passing-arguments-to-template-files-in-wordpress-5-5/](https://make.wordpress.org/core/2020/07/17/passing-arguments-to-template-files-in-wordpress-5-5/)
 27.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_query_var%2F%3Freplytocom%3D6760%23feedback-editor-6760)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_query_var%2F)
before being able to contribute a note or feedback.