Support

Account

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

Solved

Unable to write multiple values back to ACF user field

  • My project is to make a match of mentors & mentees. And then add multiple mentees.

    My mentor has an ACP current_mentees user field. I am able to add one user to that.
    But when I try to combine/add another one, it doesn’t add the 2nd one.

    // get mentor & new mentee user arrays from the Match Metadata fields
    $mentor = get_field(‘match_mentor’, $post_id);
    $mentee = get_field(‘match_mentee’, $post_id);

    //get ID from mentor array
    $match_mentor_id = $mentor[‘ID’];
    //get ID from mentee array
    $match_mentee_id = $mentee[‘ID’];

    //set up the mentor user_post_id
    $mentor_post_id = “user_”.$match_mentor_id;

    //get user array from mentor curent_users
    $current_mentees = get_field(‘current_mentees’, $mentor_post_id, false);

    // see if the current mentees is null or has a mentee already
    if ($current_mentees == ”){

    //write new current mentees back to the Mentor User Meta fields
    update_field(‘current_mentees’ , $match_mentee_id , $mentor_post_id);

    } else {

    var_dump($current_mentees); // string(2) “57”
    var_dump($match_mentee_id); // int(60)

    //combine old mentee(s) with new mentee
    array_push( $current_mentees , $match_mentee_id);

    //write new current mentees back to the Mentor User Meta fields
    update_field(‘current_mentees’ , $current_mentees , $mentor_post_id);

    I suspect there is certain type of variable that will work with all of these, I see arrays, strings, integers. Is it trying to mix them up and failing?
    Is there some consistent way to write the new user, then each additional user (array?)
    I also notice if I manually add the first one into the user account, the others will then add properly.

  • Some additional information:

    The match_mentor & match_mentee and current_mentees are all ACF fields.
    I am able to manually add (mentee) users (single & multiple) to the current_mentees through the dropdown in the user account. It appears as an array of arrays.
    The first one looks like: a:1:{i:0;s:2:”60″;}
    Adding the second one gives me: a:2:{i:0;s:2:”60″;i:1;s:2:”57″;}
    This is functional for my purpose. I’d like the code to do the same.

    Using the above code to add one user, in the current_mentees field/DB I see 60.
    And then adding the 2nd one, I get a:1:{i:0;i:57;}. (It replaces the first one).

  • Hi there,

    I’m having a really hard time trying to create a relationship between 2 post-types. I already used ACF to create another custom field which works like a charm, but yet couldn’t find a solution to this. I’m using the ACF 5.7.2 version. Thanks in advance for any help you could give.

  • Hi Michael,
    Yes, I’ve been struggling with this for a while. I have 2 situations, posting to a null field and to one that already has a user ID or array.
    I can more or less do them separately, but not both. Hoping for some help today.
    Otherwise, I’ll work on it to try and find some way past this barrier.
    And I’ll let you know if I find a solution.
    Jon

  • OK, I had thought of a couple ideas to try this morning, and things came together. Let me post my revised code. Essentially, I tried to follow the natural formatting to see if I could get everything to fall in line further along. I decided to convert the initial variable to an array, and that is what made everything else fall in line.

    // get mentor & new mentee user arrays from the Match Metadata fields
    $mentor = get_field(‘match_mentor’, $post_id);
    $mentee = get_field(‘match_mentee’, $post_id);

    //get ID from mentor array
    $match_mentor_id = $mentor[‘ID’];
    //get ID from mentee array
    $match_mentee_id = $mentee[‘ID’];

    //set up the mentor user_post_id
    $mentor_post_id = “user_”.$match_mentor_id;

    //get user array from mentor curent_users
    $current_mentees = get_field(‘current_mentees’, $mentor_post_id, false);

    // see if the current mentees is null or has a mentee already
    if ($current_mentees == ”){

    //put data in proper format
    $current_mentees = array();

    }

    //combine old mentee(s) with new mentee
    array_push( $current_mentees , $match_mentee_id);

    //write new current mentees back to the Mentor User Meta fields
    update_field(‘current_mentees’ , $current_mentees , $mentor_post_id);

  • You’re welcome. And I have a deeper understanding of these issues myself after working through them.

    Another hint, take a close look at:

    the Gravity Forms “User Registration Add-On”.

    https://www.gravityforms.com/add-ons/user-registration/

    will appear is: Form -> Settings -> User Registration

    Jon

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

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