wppaste
WordPress

get_query_var( string $query_var, mixed $default_value = '' ): mixed

Since
1.5.0, 3.9.0
Source
wp-includes/query.php:27

Reads one entry from the main WP_Query object's parsed query variables, falling back to a supplied default when the key is missing. It always inspects the global $wp_query, so results differ inside a secondary WP_Query loop built with its own arguments. Use set_query_var() when you need to write a value instead of read one.

Retrieves the value of a query variable in the WP_Query class.

Compatibility

WordPress
since 3.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$query_varstring
The variable key to retrieve.
$default_valuemixedoptional
Value to return if the query variable is not set.
Default empty string.Default: ''

Return value

mixed
Contents of the query variable.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Provide a fallback value for a query variable that was never set

Ask for a query variable that no part of WordPress registers, to see the default value take effect.

$affiliate_id = get_query_var( 'affiliate_id', 'none' );
echo esc_html( 'Affiliate ID: ' . $affiliate_id );

get_query_var() only returns the default when the key is completely absent from $wp_query->query_vars; core vars like 's' or 'paged' are already set to an empty string or zero, so the default never kicks in for those.

Read a query variable from a secondary WP_Query instead of the main query

Swap the global $wp_query for a hand built category query, read one of its query vars, then put the original query back.

global $wp_query;
$original_query = $wp_query;

$wp_query = new WP_Query( array(
	'category_name'  => 'news',
	'posts_per_page' => 5,
) );

echo esc_html( 'Category being queried: ' . get_query_var( 'category_name' ) );

$wp_query = $original_query;

get_query_var() always reads whatever object currently sits in the global $wp_query, so leaving it swapped out will break every template tag that runs after this code on the same page.

Common problems and fixes · 3

Why does get_query_var() ignore my default value and return an empty string anyway?

The function forwards straight to WP_Query::get(), which only substitutes the default when the key is entirely missing from $query_vars. Vars like 's' or 'category_name' are always present as an empty string when unused, so isset() is already true and the default never applies.

Why does get_query_var() return nothing for a custom URL parameter I added?

$wp_query only holds public query vars that WordPress knows how to parse. A key that isn't registered gets stripped out of the request before the main query ever sees it.

Why is get_query_var() empty when I call it inside a secondary WP_Query loop?

The function always reads the global $wp_query, which is the main query for the request. A secondary loop built with new WP_Query( ... ) has its own query vars stored on that object, not on the global.

Alternatives and related functions

WP_Query::get
When you already have a specific WP_Query object, such as a secondary loop, and want its own query vars rather than the main query's.
set_query_var
When you need to write or override a query variable before the main query runs, rather than read one back.
is_search
When you only need a true or false check for the current request type instead of the raw query variable's value.
get_queried_object
When you need the actual post, term, or author object behind the current query rather than a raw ID or slug.

Performance profile

How much work a call to get_query_var() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
8

Executed per call on PHP 8.5. The body compiles to 8.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
44

44 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_query_var() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always8->get()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 8 instructions, 8 executed per call, 0 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Used by · 44

Show all 44

Source code

function get_query_var( $query_var, $default_value = '' ) {	global $wp_query;	return $wp_query->get( $query_var, $default_value );}

Changelog

Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

3.9.0
The $default_value argument was introduced.from the docblock
1.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/query.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.