Update User "Field Type" for Entry in Plugin I have a channel called "Groups", with a users field type called members for the entries. I need to be able to add/remove users to the members entry field via plugin.

What I've tried is:

// criteria
$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);

// filter
$criteria-&gt;slug     = 'groupNameHere';

// section
$criteria-&gt;section('groups');

// get entry
$entries = $criteria-&gt;find();

foreach($enries as $entry)
{
    // get current members of group 
    $members = $entry['members'];

    // add new user model to $members
    $members[] = craft()-&gt;users-&gt;getUserByUsernameOrEmail("newUserEmailHere");

    // update field with new $members array
    $entry-&gt;getContent()-&gt;setAttribute('members', $members);

    // save
    craft()-&gt;entries-&gt;saveEntry($entry);
}


This simply clears the previous members value for the group, without even adding the new user. I can't find anything online about updating this type of field-type via plugin so I'm just guessing how to do this. Does anyone know the right way to do this?

Any information would hugely help! Thanks!
When a User field is saved, it takes an array of user IDs only – not complete UserModel objects. Something like this should work:

// get IDs for current members of group 
$members = $entry-&gt;members-&gt;ids();

// add new user ID to members
$members[] = $addUser-&gt;id;

// update field with new $members array
$entry-&gt;getContent()-&gt;setAttribute('members', $members);


The rest of your code looks fine, except for a minor typo – $enries should be $entries.