I am trying to change the post title before saving taking the firstname, middlenames, surname, dob and optionally dod to the title.
This works when updating a post, but when saving a new post creates a title of “- 01/01/1970”
I can’t see why this might happen unless I’m using the wrong hook maybe.
Here’s the code;
add_action( 'acf/save_post' , 'backend_after_save_post', 5);
function backend_after_save_post( $post_id ){
$posttype = get_post_type( $post_id );
if( get_post_type( $post_id ) == 'family_members' ) {
$firstname = get_field('first_name',$post_id);
$middlenames = get_field('middle_names',$post_id);
$lastname = get_field('surname',$post_id);
$dob = date("d/m/Y", strtotime(get_field('date_of_birth',$post_id)));
$dod = get_field('date_of_death',$post_id);
if($dod) {
$dod = '> ' .get_field('date_of_death',$post_id);
} else {
$dod = "";
}
$post_title = implode(' ',array_filter([$firstname,$middlenames,$lastname,'-',$dob,$dod])) ;
remove_action( 'acf/save_post', '_acf_do_save_post' );
$edit_post = array( 'ID' => $post_id, 'post_title' => $post_title );
$edit_post['post_name'] = sanitize_title( $post_title );
wp_update_post( $edit_post );
add_action( 'acf/save_post', '_acf_do_save_post' );
}
else :
endif;
}
}