[wp-hackers] rewrite base/cpt-slug/page-slug collisions?
bg at hearingvoices.com
Thu Jan 5 20:27:16 UTC 2012
in working on a Codex Docs page about Slugs,
been doing some tests and core reading that seem to reveal what might be a bug --
wanted to run it by y'all first to see if it's something i should i trac-ticket
or if it's all intentional...
1. made a Page w/ slug: 'slugname'; then a child Page with same slug: 'slugname'.
/slugname/slugname/ serves the child Page.
2. registered a heirarchical CPT w/ rewrite['slug'] of: 'slugname'.
/slugname/ serves above parent Page (query: pagename=slugs), but
/slugname/slugname/ looks for a (non-existant) CPT page,
then serves 404.php (query: slugs=slugs&post_type=slugs&name=slugs)
thing is, tho /slugname/slugname/ generates a 404,
it IS a valid Page permalink. at least that's what WP tells the user --
in the permalink box of the Edit Page screen (via get_sample_permalink()?).
however, if user clicks the View Page button, they get that CPT page 404.
this is likely due to the heirarchy of rewrite rules in which CPT rewrite slugs
get processed before Page slug matches.
3. now let's set the category_base to (you guessed it): 'slugname".
/slugname/slugname/ looks for a (non-existant) category,
then serves 404.php (query: category_name=slugs).
meanwhile WP's permalink box is telling the user, in two cases, that URL is valid:
/slugname/slugname/ the child Page AND
/slugname/slugname/ the CPT page.
however, if the user clicks the Edit screen's View button in either, they'd get the category 404.
again, likely due to rewrite rules (giving precedence to category_base over CPT rewrite slug).
meanwhile, wp_unique_post_slug() is busy checking for slugs w/ the same post_parent.
top-level Pages and top-level heirarchical CPT pages do have the same: post_parent = 0.
so if there were a top-level Page slug: 'slugname',
and you tried to assign top-level CPT page a slug of: 'slugname',
you'd get instead 'slugname-2' (b/c both have: post_parent = 0).
however those don't collide:
top-level Page: /slugname/
top-level heirarchical CPT page: /cpt-slug/slugname/
the collision comes when the CPT has the same rewrite slug as a top-level Page slug.
and further still when that same string is the category-base (or tag_base or post_format_rewrite_base).
seems like wp_unique_post_slug() should be checking not only page slugs
but also against some reserved terms ('category', 'tag', 'type')
along with any assigned rewrite bases, and registered CPT rewrite slugs.
and if post_type is hierarchical and post_parent = 0, perhaps it should allow
the same slug for different post types w/ different rewrite slugs (since there's no collision).
hope i've explained this ok. and not even sure what happens when you do things like
assign 'tag' as category base or 'type' as tag base (both are allowed in Settings).
or if permalink structure /%category%/%postname%/ when both cat and post slugs
match rewrite bases or CPT rewrite slugs.
bottom line, tho, the prob may not be in the above rewrite logic,
as much as that WP tells the user permalink info that isn't correct.
maybe wp_unique_post_slug(), get_sample_permalink(), and rewite rules should talk to each other.
and maybe register_post_type() also needs to check rewrite bases (and top-level Page slugs?).
and setting rewrite bases should also check against CPT rewrite slugs.
(and then, maybe also, it'd help telling user why they got a "-2" added to their slug/rewrite base.)
or maybe all above is fine as is, w/ good reasoning behind each process.
thanks for any insight,
~radio active transmissions~
More information about the wp-hackers