query_vars filter not working even though query string parameter is present

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

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) {
  var_dump($query->query_vars);die;
});

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 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) {
  var_dump($query->query_vars);die;
});

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?

Share Improve this question asked Oct 20, 2016 at 16:17 The Unknown DevThe Unknown Dev 3515 silver badges15 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

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'] ) ) { }

I think the value would only be present if you add the get-param to the url when trying to preview it ( or specifically set a value for the query-var using $query->set() )

Try adding ?xyz=hello-world to the end of the url you're testing on & you should see hello-world in the dump.

It will also depend on where you dump... maybe try this:

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

then visit: yoursite/?xyz=hello-world

function trythis( $wp_query ) {
    if ( $wp_query->is_main_query() && ! is_admin() ) {
        var_dump( $wp_query ); 
        exit;
    }
}
add_action( 'pre_get_posts', 'trythis' );

... & you should see 'xyz'=>'hello-world' in the dump. I don't think query vars are added to wp-query unless they need to be, that'd add unnecessary bloat.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744888746a4599276.html

相关推荐

  • query_vars filter not working even though query string parameter is present

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

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信