Adding rewrite rules
-
In this website uniquecorners.com I have created a custom post type called ‘tour’ with a taxonomy ‘destination’. I wanted the URL to be domain/destination-slug/tour-slug (for a tour page) or domain/destination-slug (for a destination page). So I wanted to avoid putting ‘tour’ in the URL.
To accomplish this, I ignored the rewrite parameters when I register the post type, as I didn’t find a way to do this. In stead, I create the extra rewrite rules myself.
I see two ways of doing it and I am not sure which is better.
This is the way I do it now (based on https://www.ads-software.com/plugins/hierarchical-urls/):
I use the filter rewrite_rules_array. I basically prepend all my rules to it.This is the alternative I am considering:
Using some action and calling add_rewrite_ruleWhat pushes me towards this alternative is that it looks more “official”. I like using the API.
What pushes me away from it is that using an action makes it execute the code every time. In the filter I am not sure it is executed every time. It might only be executed when rules are flushed (this is a complet guess).
So, my questions are:
- Is it true that this filter is only used when rules are flushed?
- What action would you use if I used the API?
- Is there an action I can use that doesn’t fire when a cached page is served (I use w3total cache)?
Thanks!!
- The topic ‘Adding rewrite rules’ is closed to new replies.