<p>You won't be able to do that using the <code>ElementCriteriaModel</code> or a single query (unless you use <code>craft ()-&gt; db-&gt; createCommand()</code> or <code>\CDbCriteria</code> to perform a conditional <code>JOIN</code>).</p>

<p>You will either have to 1) do <strong>post filtering</strong> or 2) get the <code>ids</code> for users in the groups you want and the <code>ids</code> for all admin users (which I would recommend). You can then merge both arrays and use the resulting array to get all users that match the containing <code>ids</code>.</p>

<p>You mentioned that you wanted to do it the <strong>clean</strong> way and doing a couple of queries using the public Element Query API is definitely the clean way to do it and the fact that you're doing a couple of queries to get the users you want should not feel <strong>"unclean"</strong> because that is happening in a lot of different places behind the scenes when you use the API instead of raw queries.</p>

<p>For example, when you add the <code>$ criteria-&gt;groupId = array(1, 3);</code> to a user query, Craft will hit the db once to get all <code>ids</code> for users in those groups and then hit it again as many times as necessary to do what it needs to do, like perform searches and such.</p>

<p>My point is, using the API means that you might not be able get everything in one go but your plugin won't break if Craft changes any internal APIs.</p>

CC BY-SA 3.0
2014-06-30T01:53:01.293Z
2015-07-31T23:36:18.483Z
2015-07-31T23:36:18.483Z
Selvin Ortiz
<p>I'm getting all Users from specific UserGroups like this:</p>

<pre class="lang-php prettyprint-override"><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
$criteria-&gt;groupId = array(1, 3);
$users = $criteria-&gt;find();
</code></pre>

<p>But how can I additionally fetch all admin Users?</p>

<p>I tried to add the admin attribute, but this hasn't changed anything:</p>

<pre class="lang-php prettyprint-override"><code>$criteria-&gt;admin = array(false, true);
</code></pre>

<p>I assume that's because the <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel" rel="nofollow">ElementCriteriaModel</a> has to match all attributes, but there isn't an admin which has a UserGroup with the ID 1 or 3. But how can I tell the criteria to perform a "OR" fetch?</p>

<p>P.S. I know I could merge two fetched criterias but I want to use a "clean" solution.</p>

<plugin-development><elementcriteriamodel>
How to fetch specific users OR admins with the ElementCriteriaModel?
# How to fetch specific users OR admins with the ElementCriteriaModel?

(Asked by: username_0 on 2014-06-28)

I'm getting all Users from specific UserGroups like this:
$criteria = craft()->elements->getCriteria(ElementType::User);
$criteria->groupId = array(1, 3);
$users = $criteria->find();

But how can I additionally fetch all admin Users?
I tried to add the admin attribute, but this hasn't changed anything:
$criteria->admin = array(false, true);

I assume that's because the ElementCriteriaModel has to match all attributes, but there isn't an admin which has a UserGroup with the ID 1 or 3. But how can I tell the criteria to perform a "OR" fetch?
P.S. I know I could merge two fetched criterias but I want to use a "clean" solution.

**Tags:** plugin-development, elementcriteriamodel

---

## 1 answers

### Answer by username_1 on 2014-06-30. Score: 4 | Accepted answer

You won't be able to do that using the `ElementCriteriaModel` or a single query (unless you use `craft ()-> db-> createCommand()` or `\CDbCriteria` to perform a conditional `JOIN`).

You will either have to 1) do **post filtering** or 2) get the `ids` for users in the groups you want and the `ids` for all admin users (which I would recommend). You can then merge both arrays and use the resulting array to get all users that match the containing `ids`.

You mentioned that you wanted to do it the **clean** way and doing a couple of queries using the public Element Query API is definitely the clean way to do it and the fact that you're doing a couple of queries to get the users you want should not feel **"unclean"** because that is happening in a lot of different places behind the scenes when you use the API instead of raw queries.

For example, when you add the `$ criteria->groupId = array(1, 3);` to a user query, Craft will hit the db once to get all `ids` for users in those groups and then hit it again as many times as necessary to do what it needs to do, like perform searches and such.

My point is, using the API means that you might not be able get everything in one go but your plugin won't break if Craft changes any internal APIs.


---

