<blockquote>
  <p>Find a single user by his/her affiliateId (int).</p>
</blockquote>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
$criteria-&gt;affiliateId = 1;
$users = $criteria-&gt;find();
</code></pre>

<blockquote>
  <p>Query all users within the Affiliate group, order by studentCount asc and return the top record. (I need a user that has the fewest number of students).</p>
</blockquote>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
$criteria-&gt;affiliateGroup = 'myGroup';
$criteria-&gt;order = 'studentCount ASC';
$criteria-&gt;limit = 1;
$users = $criteria-&gt;find();
</code></pre>

CC BY-SA 3.0
2015-09-28T19:52:57.897Z
Re the first code snippet of 3 lines provided above, I am getting Fatal error: Call to a member function find() on a non-object in /var/www/vhosts/.../controllers/MyController.php on line 115. I have verified that affiliateId is in fact a custom field attached to the user record.
David A McInnis
CC BY-SA 3.0
2015-09-28T19:54:51.940Z
Sorry, hadn't had coffee yet. Edited post.
Brad Bell
CC BY-SA 3.0
2015-09-29T03:36:41.050Z
With one small modification $criteria->groupId = 2;
David A McInnis
CC BY-SA 3.0
2015-09-28T17:10:14.780Z
2015-09-28T19:54:28.730Z
2015-09-28T19:54:28.730Z
Brad Bell
<p>I know how to find user by username and email. The problem comes when I need to query users based on a custom field. </p>

<p>Here is my scenario. I have about 2,000 users (part of a user group 'Affiliates') that I have created in our system. They have many custom fields including <em>affiliateId</em> and <em>studentCount</em>. I need to be able to execute two separate queries. </p>

<ol>
<li><p>Find a single user by his/her affiliateId (<em>int</em>).</p></li>
<li><p>Query all users within the Affiliate group, order by studentCount <em>asc</em> and return the top record. (I need a user that has the fewest number of students).</p></li>
</ol>

<p>I have searched docs and tried numerous combinations to query users. Any help at this point greatly appreciated. Is search the best way? If so, how do you order the search result?</p>

<plugin-development><users><usermodel>
Searching for users from a plugin
# Searching for users from a plugin

(Asked by: username_0 on 2015-09-27)

I know how to find user by username and email. The problem comes when I need to query users based on a custom field.

Here is my scenario. I have about 2,000 users (part of a user group 'Affiliates') that I have created in our system. They have many custom fields including *affiliateId* and *studentCount*. I need to be able to execute two separate queries.

1. Find a single user by his/her affiliateId (*int*).
2. Query all users within the Affiliate group, order by studentCount *asc* and return the top record. (I need a user that has the fewest number of students).

I have searched docs and tried numerous combinations to query users. Any help at this point greatly appreciated. Is search the best way? If so, how do you order the search result?

**Tags:** plugin-development, users, usermodel

---

## 1 answers

### Answer by username_1 on 2015-09-28. Score: 3 | Accepted answer

Find a single user by his/her affiliateId (int).

$criteria = craft()->elements->getCriteria(ElementType::User);
$criteria->affiliateId = 1;
$users = $criteria->find();


Query all users within the Affiliate group, order by studentCount asc and return the top record. (I need a user that has the fewest number of students).

$criteria = craft()->elements->getCriteria(ElementType::User);
$criteria->affiliateGroup = 'myGroup';
$criteria->order = 'studentCount ASC';
$criteria->limit = 1;
$users = $criteria->find();

> **Comments:**
> **username_0**: Re the first code snippet of 3 lines provided above, I am getting Fatal error: Call to a member function find() on a non-object in /var/www/vhosts/.../controllers/MyController.php on line 115. I have verified that affiliateId is in fact a custom field attached to the user record.  
> **username_1**: Sorry, hadn't had coffee yet. Edited post.  
> **username_0**: With one small modification $criteria->groupId = 2;  

---

