Title: get_query_var
Published: April 25, 2014
Last modified: February 24, 2026

---

# get_query_var( string $query_var, mixed $default_value ): mixed

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#more-information)
    - [Custom Query Vars](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#custom-query-vars)
    - [Examples](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#examples)
 * [Source](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#wp--skip-link--target)

Retrieves the value of a query variable in the [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/)
class.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#parameters)󠁿

 `$query_var`stringrequired

The variable key to retrieve.

`$default_value`mixedoptional

Value to return if the query variable is not set.
 Default empty string.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#return)󠁿

 mixed Contents of the query variable.

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#more-information)󠁿

[get_query_var()](https://developer.wordpress.org/reference/functions/get_query_var/)
only retrieves **public query variables** that are recognized by [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/).
This means that if you create your own custom URLs with their own query variables,
[get_query_var()](https://developer.wordpress.org/reference/functions/get_query_var/)**
will not retrieve them** without some further work (see below).

### 󠀁[Custom Query Vars](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#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()](https://developer.wordpress.org/reference/functions/add_query_arg/))
you need to add them to the **public query variables** available to [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/).
These are built up when [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/)
instantiates, but fortunately are passed through a filter ‘[query_vars](https://developer.wordpress.org/reference/hooks/query_vars/)‘
before they are actually used to populate the $query_vars property of [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/).

So, to expose your new, custom query variable to [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/)
hook into the ‘[query_vars](https://developer.wordpress.org/reference/hooks/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:

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

### 󠀁[Examples](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#examples)󠁿

Getting current page pagination number

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

To get the current pagination number on a [static front page](https://wordpress.org/support/article/creating-a-static-front-page/)(
Page template) you have to use the ‘page’ query variable:

    ```php
    $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](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#source)󠁿

    ```php
    function get_query_var( $query_var, $default_value = '' ) {
    	global $wp_query;
    	return $wp_query->get( $query_var, $default_value );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/query.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/query.php#L27)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/query.php#L27-L30)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Query::get()](https://developer.wordpress.org/reference/classes/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()](https://developer.wordpress.org/reference/functions/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()](https://developer.wordpress.org/reference/classes/wp_sitemaps/render_sitemaps/)`wp-includes/sitemaps/class-wp-sitemaps.php` |

Renders sitemap templates based on rewrite rules.

  | 
| [_find_post_by_old_slug()](https://developer.wordpress.org/reference/functions/_find_post_by_old_slug/)`wp-includes/query.php` |

Find the post ID for redirecting an old slug.

  | 
| [_find_post_by_old_date()](https://developer.wordpress.org/reference/functions/_find_post_by_old_date/)`wp-includes/query.php` |

Find the post ID for redirecting an old date.

  | 
| [get_the_post_type_description()](https://developer.wordpress.org/reference/functions/get_the_post_type_description/)`wp-includes/general-template.php` |

Retrieves the description for a post type archive.

  | 
| [wp_get_canonical_url()](https://developer.wordpress.org/reference/functions/wp_get_canonical_url/)`wp-includes/link-template.php` |

Returns the canonical URL for a post.

  | 
| [wp_dropdown_categories()](https://developer.wordpress.org/reference/functions/wp_dropdown_categories/)`wp-includes/category-template.php` |

Displays or retrieves the HTML dropdown list of categories.

  | 
| [paginate_links()](https://developer.wordpress.org/reference/functions/paginate_links/)`wp-includes/general-template.php` |

Retrieves paginated links for archive post pages.

  | 
| [feed_links_extra()](https://developer.wordpress.org/reference/functions/feed_links_extra/)`wp-includes/general-template.php` |

Displays the links to the extra feeds such as category feeds.

  | 
| [get_search_query()](https://developer.wordpress.org/reference/functions/get_search_query/)`wp-includes/general-template.php` |

Retrieves the contents of the search WordPress query variable.

  | 
| [single_month_title()](https://developer.wordpress.org/reference/functions/single_month_title/)`wp-includes/general-template.php` |

Displays or retrieves page title for post archive based on date.

  | 
| [wp_get_archives()](https://developer.wordpress.org/reference/functions/wp_get_archives/)`wp-includes/general-template.php` |

Displays archive links based on type and format.

  | 
| [wp_title()](https://developer.wordpress.org/reference/functions/wp_title/)`wp-includes/general-template.php` |

Displays or retrieves page title for all areas of blog.

  | 
| [post_type_archive_title()](https://developer.wordpress.org/reference/functions/post_type_archive_title/)`wp-includes/general-template.php` |

Displays or retrieves title for a post type archive.

  | 
| [WP::handle_404()](https://developer.wordpress.org/reference/classes/wp/handle_404/)`wp-includes/class-wp.php` |

Set the Headers for 404, if nothing is found for requested URL.

  | 
| [wp_old_slug_redirect()](https://developer.wordpress.org/reference/functions/wp_old_slug_redirect/)`wp-includes/query.php` |

Redirect old slugs to the correct permalink.

  | 
| [do_feed()](https://developer.wordpress.org/reference/functions/do_feed/)`wp-includes/functions.php` |

Loads the feed template from the use of an action hook.

  | 
| [get_next_comments_link()](https://developer.wordpress.org/reference/functions/get_next_comments_link/)`wp-includes/link-template.php` |

Retrieves the link to the next comments page.

  | 
| [get_previous_comments_link()](https://developer.wordpress.org/reference/functions/get_previous_comments_link/)`wp-includes/link-template.php` |

Retrieves the link to the previous comments page.

  | 
| [paginate_comments_links()](https://developer.wordpress.org/reference/functions/paginate_comments_links/)`wp-includes/link-template.php` |

Displays or retrieves pagination links for the comments on the current post.

  | 
| [get_posts_nav_link()](https://developer.wordpress.org/reference/functions/get_posts_nav_link/)`wp-includes/link-template.php` |

Retrieves the post pages link navigation for previous and next pages.

  | 
| [wp_dropdown_users()](https://developer.wordpress.org/reference/functions/wp_dropdown_users/)`wp-includes/user.php` |

Creates dropdown HTML content of users.

  | 
| [get_archive_template()](https://developer.wordpress.org/reference/functions/get_archive_template/)`wp-includes/template.php` |

Retrieves path of archive template in current or parent template.

  | 
| [get_post_type_archive_template()](https://developer.wordpress.org/reference/functions/get_post_type_archive_template/)`wp-includes/template.php` |

Retrieves path of post type archive template in current or parent template.

  | 
| [get_page_template()](https://developer.wordpress.org/reference/functions/get_page_template/)`wp-includes/template.php` |

Retrieves path of page template in current or parent template.

  | 
| [get_body_class()](https://developer.wordpress.org/reference/functions/get_body_class/)`wp-includes/post-template.php` |

Retrieves an array of the class names for the body element.

  | 
| [redirect_canonical()](https://developer.wordpress.org/reference/functions/redirect_canonical/)`wp-includes/canonical.php` |

Redirects incoming links to the proper URL based on the site url.

  | 
| [redirect_guess_404_permalink()](https://developer.wordpress.org/reference/functions/redirect_guess_404_permalink/)`wp-includes/canonical.php` |

Attempts to guess the correct URL for a 404 request based on query vars.

  | 
| [wp_list_comments()](https://developer.wordpress.org/reference/functions/wp_list_comments/)`wp-includes/comment-template.php` |

Displays a list of comments.

  | 
| [comments_template()](https://developer.wordpress.org/reference/functions/comments_template/)`wp-includes/comment-template.php` |

Loads the comment template specified in $file.

  | 
| [get_comment_link()](https://developer.wordpress.org/reference/functions/get_comment_link/)`wp-includes/comment-template.php` |

Retrieves the link to a given comment.

  | 
| [get_comment_pages_count()](https://developer.wordpress.org/reference/functions/get_comment_pages_count/)`wp-includes/comment.php` |

Calculates the total number of comment pages.

  | 
| [get_page_of_comment()](https://developer.wordpress.org/reference/functions/get_page_of_comment/)`wp-includes/comment.php` |

Calculates what page number a comment will appear on for comment paging.

  |

[Show 28 more](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.9.0](https://developer.wordpress.org/reference/since/3.9.0/) | The `$default_value` argument was introduced. | 
| [1.5.0](https://developer.wordpress.org/reference/since/1.5.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#comment-content-4408)
 2.    [turnipforest](https://profiles.wordpress.org/turnipforest/)  [  5 years ago  ](https://developer.wordpress.org/reference/functions/get_query_var/#comment-4408)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_query_var%2F%23comment-4408)
     Vote results for this note: 9[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_query_var%2F%23comment-4408)
 4.  Because [get_query_var()](https://developer.wordpress.org/reference/functions/get_query_var/)
     uses the [WP_Query](https://developer.wordpress.org/reference/classes/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).
 5.  Instead use $_GET[‘var_name’] as in typical PHP.
 6.   * We actually can use [get_query_var()](https://developer.wordpress.org/reference/functions/get_query_var/)
        outside The Loop, this function only requires the main $wp_query to be already
        intialized. See in the source code that the $[wp_query::get()](https://developer.wordpress.org/reference/classes/wp_query/get/)
        method gets the value from the $this->query_vars field, which is intialized
        on constructor and on call of init method.
      * [jorovipe](https://profiles.wordpress.org/jorovipe/) [5 years ago](https://developer.wordpress.org/reference/functions/get_query_var/#comment-4466)
      * It can also be used if the `query_var` has been registered using the [`query_vars`](https://developer.wordpress.org/reference/hooks/query_vars/)
        filter. See [note](https://developer.wordpress.org/reference/functions/get_query_var/?unapproved=6516&moderation-hash=a5f5a61b7df1d308570f6142f80f7f55#custom-query-vars)
        above.
      * [P.E.A. Lutz](https://profiles.wordpress.org/misfist/) [3 years ago](https://developer.wordpress.org/reference/functions/get_query_var/#comment-6516)
 7.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_query_var%2F%3Freplytocom%3D4408%23feedback-editor-4408)
 8.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/get_query_var/?output_format=md#comment-content-442)
 9.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/get_query_var/#comment-442)
 10. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_query_var%2F%23comment-442)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_query_var%2F%23comment-442)
 11. **Getting Current Pagination Number**
 12.     ```php
         <?php $paged = get_query_var( 'paged', 1 ); ?>
     
         <h1><?php printf( esc_html__( 'Currently browsing page %s', 'textdomain' ), $paged ); ?></h1>
         ```
     
 13. For getting the current pagination number on a static front page (Page template)
     you have to use the `page` query variable:
 14.     ```php
         <?php  $page = get_query_var( 'page', 1 );  ?>
         <h1><?php printf( esc_html__( 'Currently browsing page %s on a static front page', 'textdomain' ), $page ); ?></h1>
         ```
     
 15. Note: The query variable `page` holds the pagenumber for a single paginated Post
     or Page that includes the `<!--nextpage-->` Quicktag in the post content.
 16.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_query_var%2F%3Freplytocom%3D442%23feedback-editor-442)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_query_var%2F)
before being able to contribute a note or feedback.