I am adding a new query string parameter to be passed into the URL so that I can manipulate the REST API responses.

I added this to my functions.php

add_filter('query_vars', function ($vars) {
        $vars[] = 'xyz';
        return $vars;

This should make the parameter xyz available in the WP_Query object but it does not.

add_filter('pre_get_posts', function ($query) {

The xyz property is not available in the query_vars however if I dump out the PHP $_GET array, it is there and has the value that I passed in, so I don't know why it wouldn't be making it into the query_vars. Any ideas?


I'm not sure it quite works like that. Try inspecting $query->public_query_vars instead and I think you'll see it added in there.

The way I usually use it is like this:

add_filter( 'query_vars', 'add_test_query_vars');

function add_test_query_vars($vars){
    $vars[] = "test";
    return $vars;

So the same as you but with a named function.

Then I add a rewrite endpoint:

function wpse_243396_endpoint() {
    add_rewrite_endpoint( 'test', EP_PERMALINK );

add_action( 'init', 'wpse_243396_endpoint' );

And after that URLs ending /test/ set the query var which I test like this:

global $wp_query;    
if( isset( $wp_query->query['test'] ) ) { }
  • Thanks for the response, Andy. I'm not modifying an endpoint, though. Just trying to get it to recognize a query string parameter like. wp-json/wp/v2/posts/?xyz=some_value – Kimberly W Oct 20 '16 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.