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:


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?


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.

  • 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

This is about a year over due.

I have had a similar thing happen on my new word press install. Creating pages dynamically and getting incremental numbers on the slug names.

During this process I realised that the original slug would access the last one.

ie. ../../dynammic-page would access ../../dynamic-page-4

The solution was in the trash folder! Wordpress maintains these old files. A new page of the same name and slug name will be assigned a slug with an increment.

I permanently deleted the trashed versions of the page and the newly created dynamic page was increment free. Hazzaar!

Hope that helps someone.


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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