1

I am trying to create a new page using the wp_insert_post() function as follows:

$page = array('post_type'   => 'page'
             ,'post_title'  => 'The Resolution AR'
             ,'post_status' => 'publish'
             ,'post_author' => 1
             ,'post_slug'   => 'the-resolution-ar-2019'
);

$page_id = wp_insert_post($page);

A page does get created. Except, it gets created with the wrong post_slug.

When I look in WP_POSTS I see the post_slug as: the-resolution-ar

For some reason the function does not like the -2019 portion of the slug I am providing and automatically removes it. Any ideas?

1 Answer 1

2

Use post_name instead of post_slug (post_slug isn't valid in wp_insert_post). Just make sure you don't accidentally create pages with duplicate slugs.

1
  • 1
    Thanks! That worked.I am checking to make sure the page doesn't already exist using $page_check = get_page_by_title($page_title); and if ( !isset($page_check->ID) && !the_slug_exists($page_slug) ). Seems to be working great now!
    – mannyotr
    Commented May 10, 2018 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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