/**
* 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 );
我不是百分之百确定,但我认为问题是页面还没有ID,所以你不能更新它,你必须先插入它。