WP_Query::get( string $query_var, mixed $default = '' )

Retrieve query variable.


Parameters Parameters

$query_var

(string) (Required) Query variable key.

$default

(mixed) (Optional) Value to return if the query variable is not set.

Default value: ''


Top ↑

Return Return

(mixed) Contents of the query variable.


Top ↑

Source Source

File: wp-includes/class-wp-query.php

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

		return $default;
	}


Top ↑

Changelog Changelog

Changelog
Version Description
3.9.0 The $default argument was introduced.
1.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Sabrina Zeidan

    Check if it’s Custom post type query:

     
    if ( 'recipe' === $query->get( 'post_type' ) ) { 
    	//do your stuff 
    } 
    

    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

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

You must log in before being able to contribute a note or feedback.