WordPress get_query_var ()

I am developing a WordPress application, and I need to be able to pass url parameters using WordPress functions. I am using the add_query_arg() function to add a url parameter. However, when I try to get the passed value on another page using get_query_var() , nothing is returned. When I used $_GET['var_name'] , the values ​​are returned.

What is the possible reason for this situation? I can successfully add arguments to the URL, but I cannot access them.

+8
php wordpress
source share
2 answers

I managed to run the get_query_var() function. To use the two functions successfully, you need to add query vars to the wordpress query vars array. Here is a sample code.

 function add_query_vars_filter( $vars ){ $vars[] = "query_var_name"; return $vars; } //Add custom query vars add_filter( 'query_vars', 'add_query_vars_filter' ); 

Now you can use get_query_var() and add_query_arg() as follows:

Add var query and value

 add_query_arg( array('query_var_name' => 'value'), old_url ); 

Get the value of var request

 $value = get_query_var('query_var_name'); 

More information and code examples can be found in Codex: get_query_var and add_query_arg

+7
source share

If you check the Code, you will see that you really need to play a little to get WP to start reading the query string.

Codex (under "Custom Requests")

Excerpts:

To be able to add and work with your own custom query clocks that you add to URLs (for example: "mysite com / some_page /? My_var = foo" - for example, using add_query_arg ()), you need to add them to the variables public query available for WP_Query. They are created when WP_Query is created, but fortunately they are passed through the query_vars filter before they are actually used to populate the $ query_vars WP_Query property.

+4
source share

All Articles