Underscorefunk Design

Design, Illustration, Interaction, and Artistic Direction

add_permastruct with a struct containing %post_id% is creating endpoint rewrites, even when I specify EP_NONE

Why rewrite… why!?…

This had me vexed for a while until I followed everything all the way up to where rewrite rules are being generated. It turns out that if you include the rewrite tag %post_id% in your permastruct, WordPress will assume you’re making a permastruct for the ‘post’ post_type and will add all of the endpoints (talkbacks, comments, attachments, etc)… even if you said not to by passing EP_NONE in the add_permastruct args.

Here’s a comment from class-wp-rewrite.php around line 1070, highlighting the exact spot.

“Check to see if this dir is permalink-level: i.e. the structure specifies an individual post. Do this by checking it contains at least one of 1) post name, 2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and minute all present). Set these flags now as we need them for the endpoints.”

It basically does this check and then creates all of the endpoints. Lovely isn’t it? :/

If we take a look at the global $wp_rewrite object, you’ll see that $wp_rewrite->rewritecode contains all of the tags. There’s only one post_id like item, which is %post_id%.

The bottom line

Update: Here’s the solution I went with.

Use %p% now instead of %post_id% in your permastruct and everything should work fine.

Just thought you should know…