Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-28T19:52:57.897Z
Answers.Text: 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.
Answers.UserDisplayName: David A McInnis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-28T19:54:51.940Z
Answers.Text: Sorry, hadn't had coffee yet. Edited post.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-29T03:36:41.050Z
Answers.Text: With one small modification $criteria->groupId = 2;
Answers.UserDisplayName: David A McInnis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-28T17:10:14.780Z
Answers.LastActivityDate: 2015-09-28T19:54:28.730Z
Answers.LastEditDate: 2015-09-28T19:54:28.730Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <plugin-development><users><usermodel>
Title: Searching for users from a plugin
ThreadText: # 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;  
  
  ---
  
  