The get_query_var() WordPress PHP function retrieves the value of a query variable in the WP_Query class.
Usage
get_query_var( string $query_var, mixed $default_value = '' )
Example:
Input: get_query_var( 'paged', 1 )
Output: Retrieves the current pagination number, or 1
if not set.
Parameters
- $query_var (string): The variable key to retrieve.
- $default_value (mixed): Optional. Value to return if the query variable is not set. Default:
''
.
More information
See WordPress Developer Resources: get_query_var()
Examples
Get the current pagination number
This example retrieves the current pagination number.
$paged = get_query_var( 'paged', 1 ); echo "Currently browsing page {$paged}";
Get the current page number on a static front page
This example retrieves the current page number on a static front page.
$page = get_query_var( 'page', 1 ); echo "Currently browsing page {$page} on a static front page";
Get the post type from a query
This example retrieves the post type from a query.
$post_type = get_query_var( 'post_type', 'post' ); echo "Currently viewing post type: {$post_type}";
Get the category from a query
This example retrieves the category from a query.
$category = get_query_var( 'cat' ); echo "Currently viewing category: {$category}";
Get a custom query variable
This example retrieves the value of a custom query variable named ‘color’.
$color = get_query_var( 'color', 'red' ); echo "The selected color is: {$color}";