add_rewrite_rule query_var not being set


I have an unusual use of add_rewrite_rule (is there a usual use?) and I am getting some unexpected behavior. First here is my code:

        'index.php?mlscrit=$matches[1]/list-view/&pagename=' . $list_view_template,

add_filter('init', 'mls_declare_mls_custom_vars');

function mls_declare_mls_custom_vars() {
    add_rewrite_tag( '%mlscrit%', '(.*)' );

so as you can see I am using a pagename set within the admin dashboard to a variable called $list_view_template to limit a rewrite rule. This works fine so long as I flush rewrite rules after I update the value (which I do). The problem is with the query variable mlscrit. When I run this through Jan Fabry’s excellent rewrite analyzer,, it gives me what I expect, mlscrit = ‘/list-view/’ plus whatever is in the regex even if the regex is empty.

However if I do this:

add_action( 'template_redirect', 'mls_feed_rewrite_catch' );
function mls_feed_rewrite_catch()
      echo 'mlscrit: ' . get_query_var('mlscrit' );

and the regex has something in it, it works. But if the regex is empty, the variable also loses the ‘/list-view/’ part such that it is empty.

Can anyone explain to me why this would be the case?

I should also mention I have tried hardcoding the pagename, I have tried using the query_var filter in lieu of the add_rewrite_tag and I have tried grabbing the value of mlscrit in a bunch of different places and I always get the same answer, mlscrit is empty unless there is something in the regex.

Thank you!


, MatthewLee 8 years 2014-03-11T20:36:03-05:00 0 Answers 65 views 0

Leave an answer