2

I'm currently trying to figure out how to get my custom url rewrites working (WP Rewrite the last two parts of the URL), while doing this I came across the following problem:

I have made a custom plugin (OOP) and I'm trying to add query variables to the public wordpress query variables list.

The code I use is the following:

/**
     * Adding the trainingid var so that WP recognizes it, this is being called on the query_vars filter in the constructor.
     * @param $vars the variables coming from the filter
     * @return the same variables with the plugins query vars appended.
     */
    function my_insert_query_vars($vars)
    {
        // Push the query var id in the $vars array
        array_push($vars, 'trainingid');
        return $vars;
    }

But when I execute the following code via a shortcode on a page the array printed does not contain trainingid.

global $wp_query;
var_dump($wp_query->query_vars);

Could anybody tell me what I am doing wrong?

2
  • I take it you have add_filter('query_vars', 'my_insert_query_vars' ); (or the object-related variant) near your method? Have you made sure that filter is firing?
    – Nate Cook
    Feb 27, 2014 at 16:06
  • @NateCook It does fire, and when I print_r the $vars just before returning I see it added the variable to the array. Feb 27, 2014 at 16:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.