Search code examples
wordpressadvanced-custom-fieldsacfpro

Update field with list of user emails from ACF user field


I am hoping someone could please assist me with my below code, I am using this code to extract email addresses from an Acf user field that will have multiple users in it and the code partially works, right now it will only output one persons email address, not the email address for all of the users in the selected user field. Also, I need the list to be separated by commas.

add_action('acf/save_post', 'my_acf_save_post');
function my_acf_save_post($post_id){
    // get 'my_field' value
    $users = get_field('architect', $post_id);
    foreach($users as $user){
    $user_email = $user['user_email', ', '];
    // update 'other_field' with 'my_field' value
    update_field('architect_notification’, $user_email, $post_id);
    }
}

Solution

  • This line below overrides the same field each time.

    update_field('architect_notification’, $user_email, $post_id);
    

    That's why at the you get the one single email in that field.

    You should try something like this:

    $user_emails=[];
    foreach($users as $user){
        $user_emails[]=$user_email;
    }
    update_field('architect_notification’, $user_emails, $post_id);