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'm using wp_insert_post to dynamically generate child pages for a given page using a predefined array of values. The goal is to give each child page the same slug.

For example, clicking "Generate Pages" on "example-page" loops through wp_insert_post and creates two child pages:

/example-page/about
/example-page/contact

The trouble is that WordPress is adding numbers after the slug on the child pages (because similar child pages have already been created for a different page).

My guess is that it's doing a check on slug availability irrespective of the parent/child relationship.

I've experimented with trying to run wp_update_post after the insertion but to no avail.

Any ideas?

UPDATE

Here's a code sample:

$new_page = array(
    'post_title' => $child_page["title"],
    'post_parent' => $parent_id,
    'post_name' => $child_page["slug"], 
    'post_content' => $child_page["content"],
    'post_status' => 'publish',
    'post_type' => 'territory'
); 

$new_page_id = wp_insert_post($new_page);

Explanation: I am looping through an array of $child_pages, in which I've set the unique values for title, slug, and content.

share|improve this question
2  
show us the code you use to insert the child pages. do you explicitly set post_name and post_parent? the function responsible for uniqueness is wp_unique_post_slug, but for child pages it only has to be unique within its branch, the function queries children of its parent for matching slugs. –  Milo Feb 14 '13 at 7:20
    
I am explicitly declaring post_name and post_parent. I'll update with the code I'm using. –  Jonathan Wold Feb 14 '13 at 17:38
    
where is $parent_id set? are you sure that's a unique value each time you create the child pages? I ask because I do exactly this on a site and it works fine. you can test this manually- go to Pages in admin, create two top-level pages, then create a non-unique child page under each parent by first selecting the parent and then giving it the name. It will create identical child slugs under two different parent pages. –  Milo Feb 14 '13 at 17:47
    
The $parent_id is unique. It's a function that is triggered via AJAX (you click a "Generate Pages" button). The ID is passed into the function and it does its job. Now, I can easily go back and edit the child slugs and remove the duplicates so it does work and, you're right, I can also do it manually. –  Jonathan Wold Feb 14 '13 at 17:50
1  
Just as thought : is territory post_type hierarchical ? –  Simon Feb 14 '13 at 17:54

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.