Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
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 '13 at 21:36
add comment

3 Answers

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 )
share|improve this answer
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
    
I would like to believe that but it simply isn't the case 100% of the time for my site. –  AlxVallejo Apr 11 '12 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. –  hereswhatidid Nov 7 '12 at 15:27
    
Some code samples would greatly help in diagnosing this as well. –  hereswhatidid Nov 7 '12 at 15:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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