Retrieves the value of a query variable in the WP_Query class.
Parameters
$query_var
stringrequired- The variable key to retrieve.
$default_value
mixedoptional- Value to return if the query variable is not set.
Default:
''
Return
mixed Contents of the query variable.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).
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' );
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.
Source
function get_query_var( $query_var, $default_value = '' ) {
global $wp_query;
return $wp_query->get( $query_var, $default_value );
}
Related
Uses | Description |
---|---|
WP_Query::get()wp-includes/class-wp-query.php | Retrieves the value of a query variable. |
Used by | Description |
---|---|
build_comment_query_vars_from_block()wp-includes/blocks.php | Helper function that constructs a comment query vars array from the passed block properties. |
WP_Sitemaps::render_sitemaps()wp-includes/sitemaps/class-wp-sitemaps.php | Renders sitemap templates based on rewrite rules. |
_find_post_by_old_slug()wp-includes/query.php | Find the post ID for redirecting an old slug. |
_find_post_by_old_date()wp-includes/query.php | Find the post ID for redirecting an old date. |
get_the_post_type_description()wp-includes/general-template.php | Retrieves the description for a post type archive. |
wp_get_canonical_url()wp-includes/link-template.php | Returns the canonical URL for a post. |
wp_dropdown_categories()wp-includes/category-template.php | Displays or retrieves the HTML dropdown list of categories. |
paginate_links()wp-includes/general-template.php | Retrieves paginated links for archive post pages. |
feed_links_extra()wp-includes/general-template.php | Displays the links to the extra feeds such as category feeds. |
get_search_query()wp-includes/general-template.php | Retrieves the contents of the search WordPress query variable. |
single_month_title()wp-includes/general-template.php | Displays or retrieves page title for post archive based on date. |
wp_get_archives()wp-includes/general-template.php | Displays archive links based on type and format. |
post_type_archive_title()wp-includes/general-template.php | Displays or retrieves title for a post type archive. |
wp_title()wp-includes/general-template.php | Displays or retrieves page title for all areas of blog. |
WP::handle_404()wp-includes/class-wp.php | Set the Headers for 404, if nothing is found for requested URL. |
wp_old_slug_redirect()wp-includes/query.php | Redirect old slugs to the correct permalink. |
do_feed()wp-includes/functions.php | Loads the feed template from the use of an action hook. |
get_next_comments_link()wp-includes/link-template.php | Retrieves the link to the next comments page. |
get_previous_comments_link()wp-includes/link-template.php | Retrieves the link to the previous comments page. |
paginate_comments_links()wp-includes/link-template.php | Displays or retrieves pagination links for the comments on the current post. |
get_posts_nav_link()wp-includes/link-template.php | Retrieves the post pages link navigation for previous and next pages. |
wp_dropdown_users()wp-includes/user.php | Creates dropdown HTML content of users. |
get_archive_template()wp-includes/template.php | Retrieves path of archive template in current or parent template. |
get_post_type_archive_template()wp-includes/template.php | Retrieves path of post type archive template in current or parent template. |
get_page_template()wp-includes/template.php | Retrieves path of page template in current or parent template. |
get_body_class()wp-includes/post-template.php | Retrieves an array of the class names for the body element. |
redirect_guess_404_permalink()wp-includes/canonical.php | Attempts to guess the correct URL for a 404 request based on query vars. |
redirect_canonical()wp-includes/canonical.php | Redirects incoming links to the proper URL based on the site url. |
wp_list_comments()wp-includes/comment-template.php | Displays a list of comments. |
comments_template()wp-includes/comment-template.php | Loads the comment template specified in $file. |
get_comment_link()wp-includes/comment-template.php | Retrieves the link to a given comment. |
get_comment_pages_count()wp-includes/comment.php | Calculates the total number of comment pages. |
get_page_of_comment()wp-includes/comment.php | Calculates what page number a comment will appear on for comment paging. |
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.
Getting Current Pagination Number
For getting the current pagination number on a static front page (Page template) you have to use the
page
query variable:Note: The query variable
page
holds the pagenumber for a single paginated Post or Page that includes the<!--nextpage-->
Quicktag in the post content.