Search code examples
phpwordpresswordpress-themingcustom-wordpress-pagesquery-variables

Wordpress how to access a custom query variable from the url


I have the url http://localhost/testsite1/coder2/?id=71 with parameter id=71.

As i am developing in WordPress I cannot use $_GET['id'] and so need to use get_query_var('id') to access the parameter.

This however is not producing any result.

When I view all vars by running the code:

global $wp_query;
var_dump($wp_query->query_vars);

...I cannot see the id parameter anywhere.

C:\wamp64\www\testsite1\wp-content\plugins\CSUKCODER\csukcoder.php:92:
array (size=64)
  'page' => int 0
  'pagename' => string 'coder2' (length=6)
  'error' => string '' (length=0)
  'm' => string '' (length=0)
  'p' => int 0
  'post_parent' => string '' (length=0)
  'subpost' => string '' (length=0)
  'subpost_id' => string '' (length=0)
  'attachment' => string '' (length=0)
  'attachment_id' => int 0
  'name' => string 'coder2' (length=6)
  'page_id' => int 0
  'second' => string '' (length=0)
  'minute' => string '' (length=0)
  'hour' => string '' (length=0)
  'day' => int 0
  'monthnum' => int 0
  'year' => int 0
  'w' => int 0
  'category_name' => string '' (length=0)
  'tag' => string '' (length=0)
  'cat' => string '' (length=0)
  'tag_id' => string '' (length=0)
  'author' => string '' (length=0)
  'author_name' => string '' (length=0)
  'feed' => string '' (length=0)
  'tb' => string '' (length=0)
  'paged' => int 0
  'meta_key' => string '' (length=0)
  'meta_value' => string '' (length=0)
  'preview' => string '' (length=0)
  's' => string '' (length=0)
  'sentence' => string '' (length=0)
  'title' => string '' (length=0)
  'fields' => string '' (length=0)
  'menu_order' => string '' (length=0)
  'embed' => string '' (length=0)
  'category__in' => 
    array (size=0)
      empty
  'category__not_in' => 
    array (size=0)
      empty
  'category__and' => 
    array (size=0)
      empty
  'post__in' => 
    array (size=0)
      empty
  'post__not_in' => 
    array (size=0)
      empty
  'post_name__in' => 
    array (size=0)
      empty
  'tag__in' => 
    array (size=0)
      empty
  'tag__not_in' => 
    array (size=0)
      empty
  'tag__and' => 
    array (size=0)
      empty
  'tag_slug__in' => 
    array (size=0)
      empty
  'tag_slug__and' => 
    array (size=0)
      empty
  'post_parent__in' => 
    array (size=0)
      empty
  'post_parent__not_in' => 
    array (size=0)
      empty
  'author__in' => 
    array (size=0)
      empty
  'author__not_in' => 
    array (size=0)
      empty
  'ignore_sticky_posts' => boolean false
  'suppress_filters' => boolean false
  'cache_results' => boolean true
  'update_post_term_cache' => boolean true
  'lazy_load_term_meta' => boolean true
  'update_post_meta_cache' => boolean true
  'post_type' => string '' (length=0)
  'posts_per_page' => int 10
  'nopaging' => boolean false
  'comments_per_page' => string '50' (length=2)
  'no_found_rows' => boolean false
  'order' => string 'DESC' (length=4)

What am I doing wrong?


Solution

  • There was not enough space in the comments, so I'll explain it here!

    From the documentation:
    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.

    You could register your custom 'query_var' in wordpress by using the following filter (The code snippet goes into the functions.php file):

    add_filter('query_vars', 'registering_custom_query_var');
    
    function registering_custom_query_var($query_vars)
    {
        $query_vars[] = 'id'; // Change it to your desired name
        return $query_vars;
    }
    

    After you've registered your custom query_var, then you could access it!

    global $wp_query;
    var_dump($wp_query->query_vars);
    

    enter image description here