1

I'm trying to setup two rewrite rules for two post types. Basically, the structure I want is this:

site.com/app/course/xxxx/ => adds query_var course

site.com/app/course/xxxx/theme/xxxx/ => adss query vars course and theme

I set up two add_rewrite_rule:

public function rewrite_rules(){
    add_rewrite_tag( '%course%', '([^&]+)' );
    add_rewrite_tag( '%theme%', '([^&]+)' );    

    add_rewrite_rule("^app/course/([^/]+)/?",'index.php?page_id=3400&course=matches[1]', 'top' );
    add_rewrite_rule('^app/course/([^/]+)/theme/([^/]+)/?','index.php?page_id=3400&course=$matches[1]&theme=$matches[2]', 'top' );
}

If I add just the first rule, it works. If I only add the second rule, it works too. But, if I add both, only the first rule works. The second rule loads a valid page, but the theme var is not set.

Am I doing something wrong? Any tips are welcome!

0

1 Answer 1

0

The first rule is capturing all requests. If you add a $ anchor at the end of the rules, they'll behave as expected (you could also just switch the order of the rules so the more specific one "wins").

add_rewrite_rule(
    '^app/course/([^/]+)/?$',
    'index.php?page_id=3400&course=matches[1]', 
    'top'
);
add_rewrite_rule(
    '^app/course/([^/]+)/theme/([^/]+)/?$',
    'index.php?page_id=3400&course=$matches[1]&theme=$matches[2]',
    'top'
);

Check out Monkeyman Rewrite Analyzer for debugging rules.

Also note that theme is used by WordPress internally as a POST or GET var, I suggest changing that to something unique to prevent any potential conflict.

1
  • Thanks! Adding $ to the end of the rule works! Switching the order works too, but I prefer that structure. Also, thanks for the Monkeyman plugin. It's been a while since it has been updated, but the plugin works perfectly! Commented Mar 9, 2016 at 12:24

Your Answer

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

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