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

---

# WP_Query::get( string $query_var, mixed $default_value ): mixed

## In this article

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

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

Retrieves the value of a query variable.

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

 `$query_var`stringrequired

Query variable key.

`$default_value`mixedoptional

Value to return if the query variable is not set.
 Default empty string.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_query/get/?output_format=md#return)󠁿

 mixed Contents of the query variable.

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

    ```php
    public function get( $query_var, $default_value = '' ) {
    	if ( isset( $this->query_vars[ $query_var ] ) ) {
    		return $this->query_vars[ $query_var ];
    	}

    	return $default_value;
    }
    ```

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

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

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

Generates post data.

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

Determines whether the query is for a feed.

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

Retrieves the currently queried object.

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

Determines whether the query is for an existing post type archive page.

  | 
| [get_query_var()](https://developer.wordpress.org/reference/functions/get_query_var/)`wp-includes/query.php` |

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

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

Retrieves an array of the class names for the body element.

  |

[Show 1 more](https://developer.wordpress.org/reference/classes/wp_query/get/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_query/get/?output_format=md#)

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

| Version | Description | 
| [3.9.0](https://developer.wordpress.org/reference/since/3.9.0/) | The `$default_value` argument was introduced. | 
| [1.5.0](https://developer.wordpress.org/reference/since/1.5.0/) | Introduced. |

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

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/classes/wp_query/get/?output_format=md#comment-content-4218)
 2.   [Sabrina Zeidan](https://profiles.wordpress.org/sabrinazeidan/)  [  6 years ago  ](https://developer.wordpress.org/reference/classes/wp_query/get/#comment-4218)
 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%2Fclasses%2Fwp_query%2Fget%2F%23comment-4218)
    Vote results for this note: 0[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%2Fclasses%2Fwp_query%2Fget%2F%23comment-4218)
 4. _Check if it’s Custom post type query: _
 5.     ```php
        if ( 'recipe' === $query->get( 'post_type' ) ) { 
        	//do your stuff 
        } 
        ```
    
 6. _Usage scenario:_ if it’s a query for CPT recipe, it’s not the main query, it’s
    not admin -> say Hello! before it starts
 7.     ```php
        add_action( 'loop_start', 'wpdocs_recipes_loop_start' ); 
        function wpdocs_recipes_loop_start( $query ){
        	if ( !$query->is_main_query() && 'recipe' === $query->get( 'post_type' ) && ! is_admin() ) {
        		echo "Hello!";  	
        	}
        }
        ```
    
 8.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_query%2Fget%2F%3Freplytocom%3D4218%23feedback-editor-4218)

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