[wp-hackers] Slug Fest

Otto otto at ottodestruct.com
Sat Jan 7 19:45:43 UTC 2012


On Sat, Jan 7, 2012 at 12:41 PM, Barrett Golding <bg at hearingvoices.com> wrote:
> Q1. that function has this comment:
>  "// Page slugs must be unique within their own trees."
>
> not sure if that's correct way to say it? i think the function checks
> for identical post_name AND post_parent IN (any) hierarchical post_type
> -- so it doesn't distinguish btwn trees.

Yeah, that statement is confusing. You can have the same slug at
multiple places in a given tree, just not as siblings on the same
branch.


> while top-level page slugs in different trees DO  get a "-2" (both: post_parent=0):
> /pagename/ (Page)
> /cpt-slug/pagename-2/  (CPT page)

Actually, this particular special case might be considered to be a
bug. Don't know whether this is intentional or not. I'm guessing not.


> so, am i correct in saying?:
> All top-level parent page slugs (Pages and CPTs) must be unique.
> All Page slugs (and hierarchical CPT page slugs) with the same parent page must be unique.
>
> (i'll phase it better later for avg user, for now just want to know if i have it right).

I'd say that perhaps you're overcomplicating it. The purpose of the
numbered suffix is to ensure that you can't create pages with
conflicting permalinks. So you can't create two "/page/about" pages,
one of them will become "about-2". But "/page/about" and
"/otherpage/about" would be fine.

So ideally, the numbered suffix would appear only when there would be
a conflict.

With CPTs thrown in, I'm thinking that wp_unique_post_slug shouldn't
be checking across all post types but only for the post-type in
question. This assumes that all hierarchical CPTs defined will have a
prefix to their permalink.

It might be worth rewriting that $check_sql line to this:

$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s
AND post_type = $post_type AND ID != %d AND post_parent = %d LIMIT 1";

And seeing if that causes any issues. That would eliminate the
/cpt-slug/pagename-2 problem.


> Q2. comment reads:
> "// Attachment slugs must be unique across all types."
>
> this seems right, the SQL checks for thru all post_name(s), regardless of post_type.
> so, am i correct in saying?:
>
> Attachment slugs must be unique among all post types (including nav_menu).

Might be simpler to just say that "Attachment slugs must be unique
slugs, period." Although this isn't strictly speaking correct, since
it's possible to create an attachment with a slug before creating a
page with the same slug and have it working.

Realistically, I can't think of any actual reason that attachments
should have this requirement. I might be missing something though.


-Otto


More information about the wp-hackers mailing list