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.
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.
xK