I have a function that creates a page when I create a post (so as to construct parent-child relationship trees).

I'm using the $title of the post to create the page and this also creates a duplicate slug. Is there a way to alter the slug slightly so as to make it unique? How can I assure that the new slug will not interfere with the original post?

  • 1
    Why do you think same slug is issue in your case? Believe me, one has to bend backwards to achieve same slugs if WP core has even tiny suspicion they might conflict. :) Been there.
    – Rarst
    Jun 22, 2013 at 21:36

3 Answers 3


If you use wp_insert_post, it should calculate a unique post slug for you. If you don't use wp_insert_post, try using wp_unique_post_slug. If neither of those are working for you, you could simple try appending -page or to the new page's post_name (slug) before inserting it.

WordPress does its uniqueness check on post slugs at application level -- it doesn't look like there's a unique index on wp_posts.post_name. So, for instance, if you're inside a transaction where you're creating two posts simultaneously, the uniqueness checks would fail. It's also possible that you could create some sort of race condition where the two posts are being inserted at the same time and a uniqueness check on both slugs would fail.


I guess WP automatically appends a number at the end if the slug repeats.

  • I would like to believe that but it simply isn't the case 100% of the time for my site.
    – AlxVallejo
    Apr 11, 2012 at 21:32
  • 1
    Since you're creating the page manually via PHP rather than using the built in WordPress methods it's probably sidestepping any duplicate name checks. You'll need to check to see if that slug already exists when trying to make the page, then adjust the slug if it is already there. Nov 7, 2012 at 15:27
  • Some code samples would greatly help in diagnosing this as well. Nov 7, 2012 at 15:28

From my experience the requirement is usually opposite - achieving same slugs when WP stubbornly refuses to generate them. :)

In very few words slug might overlap between hierarchical and non-hierarchical post types, but not within them separately. Since in your case post (non-hierarchical) and page (hierarchical) are involved, WP happily allows slug reuse.

If you want to manually force WP to de-duplicate page's slug your best option is probably to force duplication check to fail and trigger numeric suffix logic. From the look at wp_unique_post_slug() source that can be done via following filter:

apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.