1

My project is to make an initial match of adoption mentor & mentee users. And then add additional mentees to that mentor.

My mentor has an ACF current_mentees (list of users) field. I am able to add one user to that. But when I try to push/add another one, it doesn't add the 2nd one properly.

// 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 & mentee array

$match_mentor_id = $mentor['ID'];  

$match_mentee_id = $mentee['ID'];

//set up the mentor user_post_id

$mentor_post_id = "user_".$match_mentor_id; 

//get mentor curent_users contents

$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 mentee back to the Mentor User Meta field

    update_field('current_mentees' , $match_mentee_id , $mentor_post_id);

       } else {

   //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);

}

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″;} //user 60 Adding the second one gives me: a:2:{i:0;s:2:”60″;i:1;s:2:”57″;} //users 60 & 57 This is functional for my purpose. I’d like the code to do the same.

But... 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 instead of adding a 2nd user).

I'm not sure if this is starting off wrong (with the first user), or if I need to modify my variables at some point before adding the 2nd (which I've tried). Thanks!

1 Answer 1

0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.