How to auto increment post title & post slug field?

I’ve a custom Post Type i.e. prj and need to auto increment it’s post_title (title) and post_name (slug) on post save, update regardless of the post status .. the post can never be deleted that’s why it won’t be an issue ..

Need it to behave just like AutoIncrement field in SQL .. once an ID is assigned, it should never be replicated ..

So far, I’ve reached to the code mentioned below .. but the problem is it is only setting up the post title, not the slug and the increment feature is not working as well ..

add_filter( ‘wp_insert_post_data’ , ‘odin_prj_title’ , ’99’, 2 );
function odin_prj_title( $data , $postarr )
{
if( $data[‘post_type’] == ‘prj’ ) {
$last_post = wp_get_recent_posts( ‘1’);
$last_post_ID = (int)$last_post[‘0’][‘ID’];
$data[‘post_title’] = ‘P0’ . ($last_post_ID + 1);
}
return $data;
}

Read more here:: How to auto increment post title & post slug field?

Leave a Reply

Your email address will not be published. Required fields are marked *