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.
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?
- Why does get_query_var() return nothing for a custom URL parameter I added?
- Why is get_query_var() empty when I call it inside a secondary WP_Query loop?
Why does get_query_var() ignore my default value and return an empty string anyway?
Why does get_query_var() return nothing for a custom URL parameter I added?
Why is get_query_var() empty when I call it inside a secondary WP_Query loop?
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
- Scaling
- Constant
- Instructions
- 8
- Plugin surface
- None
- Called by
- 38
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 8.
Nothing here hands control to plugin code.
38 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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8 | ->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 · 38
- WP::handle_404()Set the Headers for 404, if nothing is found for requested URL.
- WP_Sitemaps::render_sitemaps()Renders sitemap templates based on rewrite rules.
- _find_post_by_old_date()Find the post ID for redirecting an old date.
- _find_post_by_old_slug()Find the post ID for redirecting an old slug.
- build_comment_query_vars_from_block()Helper function that constructs a comment query vars array from the passed block properties.
- comments_template()Loads the comment template specified in $file.
- do_feed()Loads the feed template from the use of an action hook.
- feed_links_extra()Displays the links to the extra feeds such as category feeds.
- get_archive_template()Retrieves path of archive template in current or parent template.
- get_body_class()Retrieves an array of the class names for the body element.
- get_comment_link()Retrieves the link to a given comment.
- get_comment_pages_count()Calculates the total number of comment pages.
Show all 38
- get_next_comments_link()Retrieves the link to the next comments page.
- get_page_of_comment()Calculates what page number a comment will appear on for comment paging.
- get_page_template()Retrieves path of page template in current or parent template.
- get_post_type_archive_template()Retrieves path of post type archive template in current or parent template.
- get_posts_nav_link()Retrieves the post pages link navigation for previous and next pages.
- get_previous_comments_link()Retrieves the link to the previous comments page.
- get_search_query()Retrieves the contents of the search WordPress query variable.
- get_the_post_type_description()Retrieves the description for a post type archive.
- paginate_comments_links()Displays or retrieves pagination links for the comments on the current post.
- paginate_links()Retrieves paginated links for archive post pages.
- post_type_archive_title()Displays or retrieves title for a post type archive.
- redirect_canonical()Redirects incoming links to the proper URL based on the site url.
- redirect_guess_404_permalink()Attempts to guess the correct URL for a 404 request based on query vars.
- render_block_core_avatar()Renders the `core/avatar` block on the server.
- render_block_core_post_author()Renders the `core/post-author` block on the server.
- render_block_core_post_author_biography()Renders the `core/post-author-biography` block on the server.
- render_block_core_post_author_name()Renders the `core/post-author-name` block on the server.
- single_month_title()Displays or retrieves page title for post archive based on date.
- twentyfourteen_paging_nav()Display navigation to next/previous set of posts when applicable.
- wp_dropdown_categories()Displays or retrieves the HTML dropdown list of categories.
- wp_dropdown_users()Creates dropdown HTML content of users.
- wp_get_archives()Displays archive links based on type and format.
- wp_get_canonical_url()Returns the canonical URL for a post.
- wp_list_comments()Displays a list of comments.
- wp_old_slug_redirect()Redirect old slugs to the correct permalink.
- wp_title()Displays or retrieves page title for all areas of blog.
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.
Signature, return type and hooks compared across 5 parsed releases.
$default_value argument was introduced.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.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.