WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get query var

Contents

Description

Retrieve public query variable in the WP_Query class of the global $wp_query object.

Usage

<?php get_query_var$var ?>

Parameters

$var
(string) (required) The variable key to retrieve.
Default: None

Return Values

(mixed) 

returns empty string if var is not set

Examples

Getting Current Pagination Number


<?php  $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;  ?>

<h1>Currently Browsing Page <?php echo $paged; ?></h1>

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

<?php  $paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;  ?>
<h1>Currently Browsing Page <?php echo $paged; ?> On a static front 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.

Notes

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).

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 add_query_vars_filter( $vars ){
  $vars[] = "my_var";
  return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
  • See WP_Query::get()
  • Uses global: (object) $wp_query

Change Log

Since: 1.5.0

Source File

get_query_var() is located in wp-includes/query.php.

Related

Articles

Code Documentation

  • Class: WP_Query - Detailed Overview of class WP_Query
  • Object: $wpdb - Overview on the use of the $wpdb object
  • Function: get_query_var()
  • Function: query_posts() - Create additional custom query
  • Function: get_post() - Take an ID of an item and return the records in the database for that article
  • Function: get_posts() - A specialized function that returns an array of items
  • Function: get_pages() - A specialized function that returns an array of pages
  • Function: have posts() - a condition that determines whether the query returned an article
  • Function: the_post() - Used to automatically set the loop after a query
  • Function: rewind_posts() - Clears the current loop
  • Function: setup_postdata() - Sets the data for a single query result within a loop
  • Function: wp_reset_postdata() - Restores the previous query (usually after a loop within another loop)
  • Function: wp_reset_query()
  • Function: is_main_query() - Ensures that the query that is being changed is only the main query
  • Action Hook: pre_get_posts - Change WordPress queries before they are executed
  • Action Hook: the_post - Modify the post object after query
  • Filter Hook: found_posts - Changes the value of the object found_posts WP_Query
See also index of Function Reference and index of Template Tags.