Home Forums General Issues Unable to write multiple values ACF user field back to DB


Unable to write multiple values ACF user field back to DB

  • My project is to make a match of mentors & mentees, and then add the (revised) list of current mentees to the mentor user.
    (This is project that someone else started and now I am trying to get it workable.)

    I am trying to pull data from an ACF (Advanced Custom Fields/Wordpress) User field (current_mentees) that can have multiple values.
    Then I need to add a new user to this array then write it back to the current_mentees.

    I have tried a lot of different things, but am struggling with getting a variable that will then write back to the current_mentees.

    I have been successful with writing ONE user, but it overwrites and does not add to the one(s) already there. I’m looking for code help to get past this barrier.)

    $mentee = get_field(‘match_mentee’, $post_id); //ACF User field
    $mentor = get_field(‘match_mentor’, $post_id); //ACF User field
    $match_mentor_id = $mentor[‘ID’];
    $mentor_post_id = “user_”.$match_mentor_id;
    $current_mentees = get_field(‘current_mentees’, $mentor_post_id); //ACF User field (multiple)
    update_field(‘current_mentees’ , $mentee , $mentor_post_id);

    What I need to do is add:

    $new_current_mentees = $current_mentees and $match_mentee; //I have tried a variety of array_push/merge, etc.
    update_field(‘current_mentees’ , $new_current_mentees , $mentor_post_id);

    Here is what I get for the: $current_mentees = get_field(‘current_mentees’, $mentor_post_id); //2 mentee user records

    array(2) { [0]=> array(11) { [“ID”]=> int(55) [“user_firstname”]=> string(3) “Jon” [“user_lastname”]=> string(6) “Mentee” [“nickname”]=> string(23) “jhagee@frontierfolk” [“user_nicename”]=> string(22) “jhageefrontierfolk” [“display_name”]=> string(10) “Jon Mentee” [“user_email”]=> string(23) “jhagee@frontierfolk” [“user_url”]=> string(0) “” [“user_registered”]=> string(19) “2018-08-30 13:42:07” [“user_description”]=> string(0) “” [“user_avatar”]=> string(280) “” } [1]=> array(11) { [“ID”]=> int(57) [“user_firstname”]=> string(8) “Jennifer” [“user_lastname”]=> string(5) “Lee” [“nickname”]=> string(22) “jennifer.lee@uky” [“user_nicename”]=> string(21) “jennifer-leeuky” [“display_name”]=> string(14) “Jennifer Lee” [“user_email”]=> string(22) “jennifer.lee@uky” [“user_url”]=> string(0) “” [“user_registered”]=> string(19) “2018-08-30 19:24:01” [“user_description”]=> string(0) “” [“user_avatar”]=> string(280) “” } }

    2352 66 current_mentees a:2:{i:0;s:2:”55″;i:1;s:2:”57″;}
    2353 66 _current_mentees field_5a74a97e7f12c

    Thanks in advance!

  • Update:
    I was able to write the string: ‘a:1:{i:0;s:2:”57″;}’ back to the database.
    And then tried: ‘a:2:{i:0;s:2:”55″;i:1;s:2:”57″;}’ and also ‘a:3:{i:0;s:2:”55″;i:1;s:2:”57″;i:2;s:2:”60″;}’, they worked too.
    so maybe that is the way I need to fix this.
    Any suggestions for the most efficient way to code this?

    To recap:

    I pull the string from the database, may be 0 characters, on up… in that string array.
    I’ll need to add a new one to the end that and then put it back into the database.


    $old_current_mentees = ‘a:1:{i:0;s:2:”55″;}’ or ‘a:2:{i:0;s:2:”55″;i:1;s:2:”57″;}’ or ‘a:3:{i:0;s:2:”55″;i:1;s:2:”57″;i:2;s:2:”62″;}’; etc
    add new_mentee = a:2:{i:0;s:2:”55″;i:1;s:2:”68″; to the string array
    make sure it has the proper wrappers

    $new_current_mentees = $old_current_mentees + new_mentee

    write it to a variable to upload back into the DB.

    Any suggestions for the most efficient way to code this? Functions to use? Sample code?


  • It is not 100% clear by your question what type of field you’re using. If you are working with a user field, a user field stores only the User ID values in and array. The same is true of a relationship field if that is what you’re using. When you use update_field() you need to supply ACF with the value as it needs to be stored, in this case an array of ID values.

    You can get the current value unformatted by using the 3rd parameter.

    $current_mentees = get_field('current_mentees', $mentor_post_id, false);

    then you can add a new ID to the array

    $current_mentees[] = 125; // or whatever code your using to get the ID to add

    then you update the field

    update_field('current_mentees' , $current_mentees , $mentor_post_id);
  • Hi John,
    Thanks for the advice! I’ll try this out then get back with you later this morning.

  • John,

    That was what I needed, I really appreciate it!


Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Unable to write multiple values ACF user field back to DB’ is closed to new replies.