/**
* Add an auto-incrementing Project ID field to Design feedback posts
*/
function auto_assign_ids( $post_id, $post, $update ) {
// Only assign ID to new design posts
if ( $post->post_status == 'publish' && $post->post_type == 'designapprovalsystem' ) {
// get the most recent Project post
$project_args = array(
'numberposts' => 2,
'post_type' => 'designapprovalsystem',
'orderby' => 'post_date',
'order' => 'DESC'
);
$projects = get_posts( $project_args );
// get the project_id of the prior post
//get the custom field value of a post
$last_id = get_post_meta( $projects[1]->ID, 'job_number', true );
// increment
$last_id++;
// set the project_id of the current post
if ( !add_post_meta( $post_id, 'job_number', $last_id, true ) ) {
update_post_meta( $post_id, 'job_number', $last_id );
}
}
}
add_action( 'save_post', 'auto_assign_ids', 100, 3 );
I’m not 100% sure but I think the issue is that the page doesn’t have an ID yet so you can’t update it, you have to insert it first.