get_query_var( string $var, mixed $default = '' )

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


Parameters

$var

(string) (Required) The variable key to retrieve.

$default

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

Default value: ''


Top ↑

Return

(mixed) Contents of the query variable.


Top ↑

More Information

get_query_var() only retrieves public query variables that are recognized by WP_Query. This means that if you create your own custom URLs with their own query variables, get_query_var() will not retrieve them without some further work (see below).

Top ↑

Custom Query Vars

In order to be able to add and work with your own custom query vars that you append to URLs (eg: “http://mysite.com/some_page/?my_var=foo” – for example using add_query_arg()) you need to add them to the public query variables available to WP_Query. These are built up when WP_Query instantiates, but fortunately are passed through a filter ‘query_vars‘ before they are actually used to populate the $query_vars property of WP_Query.

So, to expose your new, custom query variable to WP_Query hook into the ‘query_vars‘ filter, add your query variable to the $vars array that is passed by the filter, and remember to return the array as the output of your filter function. See below:


function themeslug_query_vars( $qvars ) {
	$qvars[] = 'custom_query_var';
	return $qvars;
}
add_filter( 'query_vars', 'themeslug_query_vars' );

Top ↑

Examples

Getting current page pagination number

$paged = get_query_var( 'paged', 1 );
echo 'Currently Browsing Page ', $paged;

To get the current pagination number on a static front page (Page template) you have to use the ‘page’ query variable:

$paged = get_query_var( 'page', 1 );
echo 'Currently Browsing Page ', $paged, ' on a static front page';

Note: The query variable ‘page’ holds the pagenumber for a single paginated Post or Page that includes the Quicktag in the post content.

 


Top ↑

Source

File: wp-includes/query.php

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


Top ↑

Changelog

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

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by turnipforest

    Because get_query_var() uses the WP_Query class, which only operates within The Loop, this function cannot be used to get a url variable outside of The Loop (e.g., a WordPress admin page).

    Instead use $_GET[‘var_name’] as in typical PHP.

  2. Skip to note 2 content
    Contributed by Codex

    Getting Current Pagination Number

    <?php $paged = get_query_var( 'paged', 1 ); ?>
    
    <h1><?php printf( esc_html__( 'Currently browsing page %s', 'textdomain' ), $paged ); ?></h1>
    

    For getting the current pagination number on a static front page (Page template) you have to use the page query variable:

    <?php  $page = get_query_var( 'page', 1 );  ?>
    <h1><?php printf( esc_html__( 'Currently browsing page %s on a static front page', 'textdomain' ), $page ); ?></h1>
    

    Note: The query variable page holds the pagenumber for a single paginated Post or Page that includes the <!--nextpage--> Quicktag in the post content.

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