Getting query variables in WordPress with get_query_var

The WordPress get_query_var function retrieves a variable from the current query string, such as a custom parameter set in the URL. This can be useful for accessing and manipulating specific query variables within a WordPress template or plugin.

Using get_query_var, developers can access and use custom query variables to customize the display of content or perform specific actions based on the values of these variables.

Parameters accepted by the WordPress get_query_var function

  • $query_var (string, required): The variable key to retrieve.
  • $default_value (mixed, optional): Value to return if the query variable is not set. Default value is an empty string.

The function returns a mixed content of the query variable.


How to get the value of a custom query variable

Use the get_query_var function to retrieve the value of a custom query variable.

$custom_var = get_query_var('custom_var');
if ($custom_var) {
 echo 'The value of custom_var is: ' . $custom_var;
} else {
 echo 'Custom variable not found';

How to get the value of the paged query variable

Retrieve the value of the paged query variable using the get_query_var function.

$paged = get_query_var('paged');
if ($paged) {
 echo 'The current page number is: ' . $paged;
} else {
 echo 'Page number not found';

How to get the value of the author query variable

Use get_query_var to get the value of the author query variable.

$author_id = get_query_var('author');
if ($author_id) {
 echo 'The current author ID is: ' . $author_id;
} else {
 echo 'Author ID not found';


In conclusion, the get_query_var function is an essential tool for retrieving query variable values in WordPress. By using this function, developers can easily access and manipulate query variables within their custom templates, plugins, and themes. Understanding how to use get_query_var effectively can greatly enhance the flexibility and functionality of WordPress websites. With its simple syntax and wide range of applications, this function is a valuable asset for any WordPress developer.

Related WordPress Functions