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'] ) ) { }
Related Posts:
- how to access query string in wordpress?
- get_query_var function not working at all
- How to list some posts first in the loop based on post id
- Whats the difference between post_limits and pre_get_posts?
- How to add taxonomy filter on the query fly?
- WP_Query – filter or directly?
- What is the most efficient way of querying posts based on visits and date for current day?
- ajax category filter
- How to order posts tag by tag?
- Overriding The Loop with filter or hook
- Custom query incorrectly returning everything [closed]
- WP Rest API v2 return posts with specific tag
- How to filter sql only for a specific post type
- Ignore a filter on the media library
- How to support multiple search terms query within one process?
- How to get any tag ID
- Determine WP_Query parameters from URL
- Which filter/action hook gets triggered after a query has been performed?
- Removing posts from the returned wp_query. Cannot use object of type WP_Query as array
- Why query_vars get altered in WP_Query Object?
- How to implement time filter to show random post 1 month for one category and 3 months for other categories
- What’s the point of the query_vars filter?
- Refine/Filter WP_Query Posts with jQuery or Ajax
- How to apply filter inside a single wp_query?
- What is considered a default query for the request filter?
- Modifying the Search Results
- When to add_filter() to Custom Query
- Order by custom table and multiplication
- Query total number of posts
- Rewrite rule to prettify two $_GET variables while in a new endpoint from a page
- How to add post_distinct filter to WP_Comment_Query?
- Using Ajax to load more posts | Help me change the wording on my button to notify the user
- Changing WP_Query params with url Query Var
- How to use filter hook “the_posts” with a function that refers to $this?
- How to create a WordPress Post Filter that users can use to filter posts by year?
- add_filter() depending on search term
- How do I run through a WordPress loop called from a filter function?
- Order by title without taking into account ‘the’
- wp_query ‘s’, search filter with pagination is not working
- How to filter, restrict and return posts based on custom user meta information
- Bypass “supress_filters” in WP Query
- How to use the query hook/filter?
- Filter posts in category archive page by year using a dropdown
- Display filtered results into custom page
- add_filter(‘query_vars’) not working in custom template
- Applying posts_clauses filter to specific queries only
- Add class to first post in custom loop using post_class filter
- How to get a query variable?
- How to alter local query, not main query [duplicate]
- How to filter a query by date in a shortcode?
- How can I allow sticky posts but cap the query to 1 post?
- Query Vars Not Set in Main Query
- Filter Custom Taxonomy Posts
- Include posts from some categories while excluding from others
- Changing the default wp_search_stopwords
- Filter post query to only show direct children of category
- Is it possible to add an argument to a custom function added to a filter hook?
- How to make posts unqueryable/unpublish posts where ACF relationship field is an unpublished post? [closed]
- Query_vars support in Rest API
- Change AJAX filter from POST to GET for URL Parameters
- Using post_where filter only on main query NOT sidebard new WP_Query
- Filtering posts by archive showing all years
- Remove post type filter added by the plugin in the final query
- relation OR instead of AND – Filtered term ID’s in loop
- Can’t order the query result as given posts id’s array
- filter posts by meta key with pagination
- Why does apply_filters behave different inside and outside a loop?
- meta_query weird behaviour, static int will work, but not user data
- Filter sub-category from checkbox form
- My combination of ‘post_type’ and ‘tax_query’ not working?
- How to add a “base” filter for all posts visible to visitors on the site?
- WordPress – Form does not filter the results of taxonomies
- Query Posts | Combining multiple form inputs into query arguments to generate one filter result
- Sorting query_posts() with a complex orderby filter
- WP Query filters active on wrong query
- Custom category page with taxonomy filters
- Gather same custom field values in one value in a select tag with wp_query
- get term objects and post objects in query
- How to set selected attribute on option after filter query?
- Own query vars are not displayed
- Issue generating custom filter for cpt admin columns
- WP_Query doesn’t works inside loop
- How to redirect a query string to a “virtual” subdirectory
- Fetch only categorized posts
- Custom query with post_class filter using current_post not working
- Add filter to Orderby Parameter using Array
- Filter WP_Query output before it is accessed (pre_get_posts)?
- Passing query variables to a custom page template
- Loop the posts between two dates selected using jquery datepicker
- query post based on comparison
- Do not show a custom post if it belongs to a certain custom taxonomy
- wp_query->set for several categories
- Adding arguments to WooCommerce Product Loop using AJAX
- Edit WordPress loop, taxonomies, & filter
- query_vars category_name only display one catgory out of multiple categories
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- List categories using WP_Query
- I need to get all categories from a WP_Query
- Send a variable with a URL and get a variable with Wp query
- WP_Query filter Posts by timestamp event (range start and end) and by month (next 12 month)