You don’t have to think about it – WordPress will take care of this.
Let’s take a look at wp_insert_post
source code…
On line 3203 you’ll find:
if ( empty($post_name) ) {
if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
$post_name = sanitize_title($post_title);
} else {
$post_name="";
}
} else {
// On updates, we need to check to see if it's using the old, fixed sanitization context.
$check_name = sanitize_title( $post_name, '', 'old-save' );
if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) {
$post_name = $check_name;
} else { // new post, or slug has changed.
$post_name = sanitize_title($post_name);
}
}
So if no post_name
is set, WP will generate it from post_title
.
And then on line 3325:
$post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
So WP will take care of uniqueness of post_name
.