WP No slug when adding a pending post

I’m using the following snippet of code to add a post:

$post_args = array(
    'post_content'   => 'test test',
    'post_name'      => 'slughere',
    'post_title'     => 'title',
    'post_status'    => 'pending',
    'ping_status'    => 'closed',
    'comment_status' => 'closed'
);

$new_post_id = wp_insert_post( $post_args );

If I add a post with a ‘publish’ status, it works. But when I post it as a ‘pending’ post, for some reason the slug is not being added. It just stays empty.

Read More

Does anybody have any idea how this is possible of why this is happening?

Running the latest WP and even updated the core again to be sure there is nothing weird going on there.

Related posts

Leave a Reply

1 comment

  1. If you want to add the parameter post_name manually make sure to sanitize and assure uniqueness,

    So try following code:-

    $post_args = array(
            'post_content'   => 'test test',
            'post_name'   => wp_unique_post_slug( sanitize_title( 'slughere' ) ),
            'post_title'     => 'title',
            'post_status'    => 'pending',
            'ping_status'    => 'closed',
            'comment_status' => 'closed'
    );
    
    $new_post_id = wp_insert_post( $post_args );
    

    Hope this will help.