1

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?

Ruvee
  • 8,611
  • 4
  • 18
  • 44
sw123456
  • 3,339
  • 1
  • 24
  • 42
  • It's okay, in `query_vars()` shouldn't be ID - https://codex.wordpress.org/WordPress_Query_Vars – pavel Jan 18 '22 at 18:52
  • I thought that would list all available vars that can be accessed via get_query_var()? All was working fine with $_GET but I needed to add rewrite rules and so had to alter the way I accessed the parameters, but the suggested solution is not returning the parameter. – sw123456 Jan 18 '22 at 18:54

1 Answers1

6

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

Ruvee
  • 8,611
  • 4
  • 18
  • 44
  • 1
    Absolutely brilliant. Thank you so much for your help! – sw123456 Jan 18 '22 at 21:11
  • @Ruvee, for some odd reason, my custom query_vars don't appear in the global list. To test this, I added the example you have here and it still isn't in the list. Any ideas on why it isn't included? – Motivated Jun 28 '23 at 00:27
  • @Motivated Thanks for reaching out. I honestly have no idea as why you can't get this code to work. It could be due to many reasons that I don't know about if you don't elaborate! It could be the plugins you have installed on your wordpress installation or it could be the version of wordpress that you're running on your server, or maybe you're problem is something else. Just by saying "This or that doesn't work", and not explaining clearly and not providing error(s)/warning(s) you got, is not a good way to debug software. You would need to elaborate, so that other people could help you. – Ruvee Jul 21 '23 at 16:30