I have created a custom permalink structure for a custom post type (events) using the code below:
$GLOBALS["wp"]->add_query_var("event_year");
$GLOBALS["wp"]->add_query_var("event_monthnum");
$GLOBALS["wp"]->add_query_var("event_day");
$GLOBALS["wp_rewrite"]->add_rewrite_tag("%event%", "([^/]+)", "event=");
$GLOBALS["wp_rewrite"]->add_rewrite_tag("%event_year%", "([0-9]{4})", "event_year=");
$GLOBALS["wp_rewrite"]->add_rewrite_tag("%event_monthnum%", "([0-9]{1,2})", "event_monthnum=");
$GLOBALS["wp_rewrite"]->add_rewrite_tag("%event_day%", "([0-9]{1,2})", "event_day=");
$GLOBALS["wp_rewrite"]->add_permastruct("event", "/event/%event_year%/%event_monthnum%/%event_day%/%event%", false);
For the most part this works just as expected. A problem however arises when I try to use pagination on a URL with a year, month and date. In which case it is assumed that "page" is the value for %event%.
I used the Monkeyman Rewrite Analyzer plugin where I noticed the required pattern (event/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$
) is listed, but only after the pattern that tries to use the page argument as event name (event/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)(/[0-9]+)?/?$
).
I also noticed that calling $GLOBALS["wp_rewrite"]->rewrite_rules()
after the rewrite definitions will make pagination work but breaks URLs with event names (somehow event_year, event_monthnum, event_day and event translate to $1, $2, $3 and $4 in that case).
Calling $GLOBALS["wp_rewrite"]->flush_rules()
did make everything work as I required but that wouldn't be a good solution.
Is there some way I could flip the patterns so that the pattern that looks for a page comes before the pattern that looks for a post name?