Support

Account

Home Forums General Issues How to update user field with correct object etc

Solved

How to update user field with correct object etc

  • Not an ‘issue’, just a question:
    Would appreciate a point in the right direction here:

    So I have a custom post type which are decisions to be made by a company. I have a field which are all the people required to give their input on the decision, and then a field for those who voted yes and those who voted no. As users vote, they are to be moved from 1 field to the other.

    The ‘user’ field type in ACF only saves the user object, not just the ID’s. So the object that it saves looks like this:

    So I’d planned to get the array index of the user who’s casting their decision, and then remove them and add them to the other field.

    If we were just working with ID’s here, that’d be fine. But I have to work with full objects and here’s my problem:

    The object created by WordPress’s get_user_by() function returns a very different looking object. So I’m assuming I wont be able to save this object into the ACF field, and neither will I be able to find the index because the objects are so very different.

    Ideas to how I could achieve this better/easier?

  • just a follow-up thought… I guess i can remove the user by finding the ID in the main array and then removing that item…

    but still not sure how to programmatically add a user to one of the other fields, considering how different the user objects are?

  • Ok, I’ve been hacking away and I’ve got the updating of the arrays working perfectly, but it’s not saving data correctly – I’m seeing that the data is serialised, so other than saving a new array, I’m not sure how this should be done?

    // retrieved variables from Ajax request
    $user = $_REQUEST['user'];
    $decision = $_REQUEST['decision'];
    $decision_id = $_REQUEST['decision_id'];
    
    // get current decision info
    $this_decision = get_field('decision_makers', $decision_id);
    // echo '<pre>';print_r(array_search($user, array_column($this_decision, 'ID')));echo '</pre>';;
    
    // store the changed user in a var
    $moved_user = $this_decision[array_search($user, array_column($this_decision, 'ID'))];
    // echo '<pre>';print_r($moved_user);echo '</pre>';
    
    // remove user from decision_makers
    unset($this_decision[array_search($user, array_column($this_decision, 'ID'))]);
    echo 'new updated decision';
    echo '<pre>';print_r($this_decision);echo '</pre>';
    update_field('field_575e67c05fc55', $this_decision, $decision_id);
    
    // add user to either decision_yes or decision_no
    switch ($decision) {
    	case 'yes' :
    		$yes_users = get_field('decision_yes', $decision_id);
    		array_push($yes_users, $moved_user);
    		update_field('field_575fee49f5980', $yes_users, $decision_id);
    
    		echo 'new yes users';
    		echo '<pre>';print_r($yes_users);echo '</pre>';
    		break;
    	case 'no' :
    		$no_users = get_field('decision_yes', $decision_id);
    		array_push($no_users, $moved_user);
    		update_field('field_575fee6bf5981', $no_users, $decision_id);
    		
    		echo 'new no users';
    		echo '<pre>';print_r($no_users);echo '</pre>';
    		break;
    }

    It’s working perfectly in that the last user – the gryphon avatar which is me, WAS a part of the first array, but on running this function, it moved me from the ‘new updated decision’ array, into the ‘new yes users’ array…

    Perfect..

    But on trying to save these updated arrays into ACF, it’s borking out:

    update_field('field_575fee49f5980', $yes_users, $decision_id);

    Do I need to handle the serialisation?

  • Backing up a bit to your OP, the user field only stores the user ID in the database. It just returns a user object when you get the value. You can get the unformatted value of the field, an array of user IDs, by using the 3rd parameter in ACF $this_decision = get_field('decision_makers', $decision_id, false); I’m pretty sure that you can also just use an array of user IDs in update field.

  • Ah really? Thanks @hube2

    So in order to save data back into that field using update_field, I’d just pass an array of ID’s (not entire user objects) into the field?

  • I have never used update_field() on a user field. But I have used it on other relationship type fields and have always just used the array of IDs I want to update it to……

    I just looked at the code for update field. You can use wither user objects or user IDs, or even an array with user data. If you pass it an array or an object it converts it to the bare ID.

  • Hmm… Well I was passing an array (the same user array that it stores (well that it seemed to store)) and it wasn’t saving… it was saving the user ID 1 to every field it updated.

    So not sure about it working with arrays…

    But I’ll try tomorrow with a fresh mind to store just the user ID array and see what happens…

    Will update.
    Thanks!

  • I’m not saying you’re wrong because I’m not actually testing this with code, but I’ve followed the code for update field and don’t see anything that should be turning the array into a single value. So I’m a bit confused by it as well.

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

You must be logged in to reply to this topic.