WordPress.org

Ready to get started?Download WordPress

Forums

wp_unique_post_slug question (1 post)

  1. yp.gabel
    Member
    Posted 1 year ago #

    Hi everyone,

    Don't know if it's the right place to post so sorry if it is not.

    I stumbled upon a problem during a website development.

    In short, I need various Custom Post Types to be hierarchical, so that URL match the following permalink structure :

    domain.com/cpt_slug/[parent_slug_if_any]/children_slug

    So I created all the CPT I needed, and went on posting some pages on each one of them for testing purpose.
    Problem is, I can have the same page slug across different post type. For instance, let's take a FAQ page :

    domain.com/cpt_slug/faq
    domain.com/other_cpt_slug/faq

    Both this URL should be valid. But when I save my post in my different post types, I get the second one named "faq-2".
    If I switch my CPT to be non-hierarchical, the problem is gone and I can save two pages with the same slug, as long as they are saved in different post types. Given the fact that I need my posts to be able to have parents, that solution did not satisfy me.

    So I dug a little bit in wordpress core code and found out there's a function dedicated to the attribution of the slug : wp_unique_post_slug (wp-includes/post.php - line 3073).

    On line 3100 you can see a test that checks if the post type of the post we want to generate the slug for is a hierarchical one. If it is (that's my case), it looks in the database if a post with the same slug already exists (so far so good, that's logic). What I find a little bit odd is that it goes looking ACROSS ALL POST TYPES and not only in the one we want to save.
    On the other hand, when the post is not hierarchical, it only checks within the current post type and not the others (which explains why when I switched my post type from hierarchical to non-hierarchical, it worked the way I want it)

    My question is simple : Why ?
    Why does it look across all registered post type to avoid slug duplication ? Especially when there's a comment (line 3103-3104) that says :

    // Page slugs must be unique within their own trees. Pages are in a separate
    // namespace than posts so page slugs are allowed to overlap post slugs.

    If both my FAQ pages have the slug "faq" while being in different post type, my page slugs are unique within their own tree, are they not ?

    For the time being, I'm using the filter wp_unique_post_slug to apply my own logic (hierarchical post type posts can have the same slug if in different post types) but I would like to know if there's any side effects I might stumble upon by applying such logic ?
    Is there a reason why hierarchical posts can't have the same slug across different post types ? because so far, with the logic I'm applying, it works pretty well.

    Thanks in advance for the answer and if you need any precision, do not hesitate !

    Cheers.

Topic Closed

This topic has been closed to new replies.

About this Topic