I have a field in my users of type user. This field holds a user's employer (another user).

How can I get a collection of all the employees in a plugin?

I tried craft()->users->relatedTo($currentUser); (where $currentUser is the employer) as that's pretty much how I'd do it in a template, but of course relatedTo isn't part of the usersService.

So how can I do this without just getting every user, checking their employer field and filtering out the matches?


ElementCriteriaModel supports 'users' as well as 'entries'.

$criteria = craft()->elements->getCriteria(ElementType::User);
$criteria->relatedTo = array(
    'targetElement' => $currentUser,
    'field'         => $employer
$employees = craft()->elements->findElements($criteria);
| improve this answer | |
  • Thanks for that. I've used this technique loads of times, was having a major brainfart yesterday! Accepted as the answer. – billythekid Jan 6 '15 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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