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.
post_nameandpost_parent? the function responsible for uniqueness iswp_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 at 7:20post_nameandpost_parent. I'll update with the code I'm using. – Jonathan Wold Feb 14 at 17:38$parent_idset? 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 at 17:47$parent_idis 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 at 17:50territorypost_typehierarchical ? – Simon Feb 14 at 17:54