slug – How to prevent post_name from incrementing in wp_insert_post?

Question

Whenever I use wp_insert_post with an integer or a stringified integer for the post_name value, it adds a hyphen and an incremental counter to the end of the value. This happens regardless of whether or not the value was a duplicate. It does not, however, happen if I give it a post_name string that does not have an integer representation.

wp_insert_post(array(
    'post_name' => 2766875,
    'post_type' => 'example',
    'post_status' => 'publish',
    'post_content' => '',
    'post_title' => 'erfgdegvsgrser',
))

This will result in a post being inserted with the post_name of ‘2766875-2’. Why does it keep adding this incremental number, even when there are no previous posts with a post name of 2766875 in the database? How do I prevent it from doing so?

This appears to be a problem with the wp_unique_post_slug function as well. If I feed this to this function, I also get an incremented result, even though the original post_name was unique.

wp_unique_post_slug(2766875, 62671, 'publish', 'example', 0);

result:

2766875-2

0
kloddant 1 month 2021-12-10T15:21:38-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse