1

I have this code below which works perfectly, i.e. when adding a new User a post is programatically created in my CPT.

add_action( 'user_register', 'add_member_employees' );

    function add_member_employees( $user_id ) {

    $user_meta=get_userdata($user_id); $user_roles=$user_meta->roles; 
    if (in_array("employees", $user_roles)){

    $new_member = array(
    'post_title' => wp_strip_all_tags( $_POST[ 'first_name' ] . " " . $_POST[ 'last_name' ] . " - "  .  date( 'F j, Y', current_time( 'timestamp' )) ),
    'post_author' => $user_id,
    'post_type' => 'employee',
    'post_status' => 'publish',
    'meta_input' => array(
    'wpcf-employee-name' => $_POST[ 'first_name' ] . " " . $_POST[ 'last_name' ],
    'wpcf-employee-reg-date' => date( current_time( 'timestamp' )),
    ),
    );
wp_insert_post( $new_member );
}

}

What I'd like to do is if the User last_name is updated to then 'automatically' update just the post_title and post_name with the change of last name. I've looked at wp_update_post and googled a lot, but I've confused myself.

BTW the wpcf-employee-name is a Types field (Toolset plugin) and I'd like to update the last_name here too.

Could I in essence replicate the above but use wp_update_post? If this was the case would the slug be created using the code below (not sure about the date format):

'post_name' => wp_strip_all_tags( $_POST[ 'first_name' ] . "-" . $_POST[ 'last_name' ] . "-"  .  date( 'F j, Y', current_time( 'timestamp' )) ),

I'm not experienced when it comes to coding. So apologies if these are naive questions. Thank you for any help at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.