Title: query_vars filter doesn&#039;t work (?)
Last modified: August 19, 2016

---

# query_vars filter doesn't work (?)

 *  Resolved [ejankowski](https://wordpress.org/support/users/ejankowski/)
 * (@ejankowski)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/)
 * Hi there,
 * I’m creating a plugin which need to register a new query var. I wrote this code
   in the main plugin file
 *     ```
       function add_my_query_var($vars) {
   
       $vars[] = 'my_var';
       return $vars;
       }
   
       add_filter('query_vars', 'add_my_query_var');
       ```
   
 * But if I do, in any of my templates file, like page.php. There is no “my_var”.
 *     ```
       global $wp_query;
       var_dump($wp_query->query_vars);
       ```
   
 * What am I doing wrong?
 * Thanks!!

Viewing 15 replies - 1 through 15 (of 26 total)

1 [2](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/page/2/?output_format=md)

 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/#post-1865018)
 * You have to pass a value for my_var in before it will work
 * What you’ve done so far is correct
 * A) Add to functions.php
 *     ```
       add_filter('query_vars', 'lg_queryvars' );
       function lg_queryvars( $qvars ) {
         $qvars[] = 'productID';
         return $qvars;
       }
       ```
   
 * b) **pass the parameter on the URL in the link in the program where you need 
   it**
    `<a href="/shop/?product_id=$variable_name">click here</a>`
 * c) in the shop page retrieve the query var value
    `$productID = $wp_query->query_vars['
   productID'];`
 *  Thread Starter [ejankowski](https://wordpress.org/support/users/ejankowski/)
 * (@ejankowski)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/#post-1865257)
 * Thanks a lot for the answer!
 * I was doing something wrong.
 * Thanks!
 *  [csam0003](https://wordpress.org/support/users/csam0003/)
 * (@csam0003)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/#post-1865375)
 * Hi, in the themes folder I have two functions.php files; where functions.php 
   file includes functions-custom.php file.
 * I placed
 *     ```
       add_filter('query_vars', 'lg_queryvars' );
       function lg_queryvars( $qvars ) {
         $qvars[] = 'productID';
         return $qvars;
       }
       ```
   
 * in functions-custom.php file and in a custom plugin which a particular page uses,
   I included
 *  `$productID = $wp_query->query_vars['productID'];`
 * I then have an external website that contains a link such as [http://www.mysite.com/form/?product_id=24](http://www.mysite.com/form/?product_id=24),
   however it seems not to work.
 * Any ideas?
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/#post-1865376)
 * You are setting up a variable called `productID`
    `$qvars[] = 'productID';`
 * But you are passing on the URL a variable called `product_id`
 * While similar (in English), to PHP `productID` and `product_id` are two different
   variables. Make them both the same and it should work.
 *  [csam0003](https://wordpress.org/support/users/csam0003/)
 * (@csam0003)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/#post-1865377)
 * thank you
    yeah…still doesnt work…maybe I have to include the first part of the
   code in functions.php file and not functions-custom.php file?
 * Should the classes.php file in the includes folder which contain such variables
   show that a query variable has been added?
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/#post-1865378)
 * You just need to try different things and eliminate them one by one. The questions
   you are asking are theme-specific and depend on how the theme/framework you are
   using is set up.
 * So you can conceptualize what is going on:
 * $qvars is a global array that WordPress keeps that contains all the valid URL
   query strings that WordPress is interested in keeping. Basically WP deletes any
   query vars passed on the URL that it doesn’t find in that array. What you are
   attempting to do is add your desired query var to the array of query vars that
   WordPress knows about and keeps.
 * When I was learning how to do this I found there is a second way to do it, which
   I found easier actually.
    [http://www.simonwheatley.co.uk/2009/02/13/adding-get-params-to-a-url-in-wordpress/](http://www.simonwheatley.co.uk/2009/02/13/adding-get-params-to-a-url-in-wordpress/)
   Maybe that alternate approach will help you.
 *  [csam0003](https://wordpress.org/support/users/csam0003/)
 * (@csam0003)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/#post-1865379)
 * Thanks for your quick replies stvwlf!
 * Should the classes.php file be affected when the new query variable is registered?
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/#post-1865380)
 * I think you mean classes.php that is part of your theme. It shouldn’t affect 
   anything but that is theme dependent. Go ahead and try it. Its’ not going to 
   permanently change classes.php so if it breaks anything just take out the code
   you added.
 * Also try the method in the article I linked to. It doesn’t require changing the
   query vars array.
 *  [csam0003](https://wordpress.org/support/users/csam0003/)
 * (@csam0003)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/#post-1865381)
 * The thing is add_query_arg(); is used to add the parameters when knowing the 
   value of the query.
 * My situation is this. I have 3 external websites that are not wordpress. each
   website has a link to the same forms page on my wordpress site with a reference
   parameter…so
 * external site 1 : [http://www.mywpsite.com/forms/?refId=123](http://www.mywpsite.com/forms/?refId=123)
   
   external site 2 : [http://www.mywpsite.com/forms/?refId=456](http://www.mywpsite.com/forms/?refId=456)
   external site 3: [http://www.mywpsite.com/forms/?refId=789](http://www.mywpsite.com/forms/?refId=789)
 * On the forms page, the fields are inputted and on submit a plugin is called which
   takes the form fields aswell as the refID and populates an external database.
 * Is there a way of checking in some file if the query variable has been registered?
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/#post-1865382)
 * I just tested this and it worked fine.
 * your code in functions.php should be
 *     ```
       add_filter('query_vars', 'my_queryvars' );
       function my_queryvars( $qvars ) {
         $qvars[] = 'refId';
         return $qvars;
       }
       ```
   
 * I put
    `<?php var_dump($wp_query->query_vars); ?>` near the top of the page template
   that is used to display the page at URL [http://www.mywpsite.com/forms/?refId=123](http://www.mywpsite.com/forms/?refId=123)
 * When I viewed a URL that had ?refId=123 at the end of the URL, the first item
   in the array that displayed was
 *     ```
       array(56) {
         ["refId"]=>
         string(3) "123"
       ```
   
 * you can access the value of the query variable like this
    `$refId = $wp_query-
   >query_vars['refId'];`
 * If I don’t have a query string on a URL, refId is not in the array. Its only 
   appearing when it is passed on the URL.
 * Does this help?
 *  [csam0003](https://wordpress.org/support/users/csam0003/)
 * (@csam0003)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/#post-1865383)
 * mmmm interesting…first of thank you very much for your help. Im a wordpress newbie.
 * I included <?php var_dump($wp_query->query_vars); ?> at the top of the template
   and getting NULL value
 *  [csam0003](https://wordpress.org/support/users/csam0003/)
 * (@csam0003)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/#post-1865384)
 * just a quick question should I declare $wp_query as global in the page template?
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/#post-1865385)
 * Depends where in what template file you put it in. I placed it in index.php after
   the #content div and didn’t need to declare it as global.
 * No harm done declaring it as global – that would be the next thing to try
 *  [csam0003](https://wordpress.org/support/users/csam0003/)
 * (@csam0003)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/#post-1865386)
 * yeah…I think its just not registering for some strange reason.
    The fact that
   is NULL means that I guess its not finding the query variable.. Im lost 🙁
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/#post-1865387)
 * the var dump is just to confirm that the value is being picked up. it has nothing
   to do with whether the function is working.
 * The issue is are you seeing a value in $wp_query->query_vars[‘refId’] when you
   use it on a page with the URL
    [http://www.mywpsite.com/forms/?refId=123](http://www.mywpsite.com/forms/?refId=123)
 * If the answer is No, I suggest you temporarily load the TwentyTen default theme–
   add the function to its functions.php file and call the same URL. See if it is
   working in the default theme. If it works there and not in your theme you know
   the issue is your theme. Then you can contact the theme author for suggestions.
 * I used the TwentyTen theme here when I was testing and I assure you it worked
   exactly as I said.

Viewing 15 replies - 1 through 15 (of 26 total)

1 [2](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/page/2/?output_format=md)

The topic ‘query_vars filter doesn't work (?)’ is closed to new replies.

 * 26 replies
 * 3 participants
 * Last reply from: [csam0003](https://wordpress.org/support/users/csam0003/)
 * Last activity: [15 years, 3 months ago](https://wordpress.org/support/topic/query_vars-filter-doesnt-work/page/2/#post-1865398)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
