<p><code>ElementCriteriaModel</code> supports 'users' as well as 'entries'.</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
$criteria-&gt;relatedTo = array(
    'targetElement' =&gt; $currentUser,
    'field'         =&gt; $employer
);
$employees = craft()-&gt;elements-&gt;findElements($criteria);
</code></pre>

CC BY-SA 3.0
2015-01-06T10:28:38.703Z
Thanks for that. I've used this technique loads of times, was having a major brainfart yesterday! Accepted as the answer.
billythekid
CC BY-SA 3.0
2015-01-05T17:43:18.510Z
2015-01-05T17:43:18.510Z
Douglas McDonald
<p>I have a field in my users of type user. This field holds a user's employer (another user).</p>

<p>How can I get a collection of all the employees in a plugin?</p>

<p>I tried <code>craft()-&gt;users-&gt;relatedTo($currentUser);</code> (where <code>$currentUser</code> 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.</p>

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

<plugin-development><relations>
How can I find users by field value?
# How can I find users by field value?

(Asked by: username_0 on 2015-01-05)

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?

**Tags:** plugin-development, relations


---

## 1 answers

### Answer by username_1 on 2015-01-05. Score: 4 | Accepted answer

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

> **Comments:**
> **username_0**: Thanks for that. I've used this technique loads of times, was having a major brainfart yesterday! Accepted as the answer.  

---

