Answers.Body: <p>To get a user by a custom field, you would need to create a ElementCriteriaModel like this:</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
  $criteria-&gt;fieldName = craft()-&gt;request-&gt;getPost('fieldName');
  
  $users = $criteria-&gt;find();
  </code></pre>
  
  <p>you could then loop over $users and use getAttributes() to get the user data, and if you need the attribute names use attributeNames() if you need the users fields. </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-12T14:49:11.183Z
Answers.Text: Hi Luke, how would you do this in Craft 3? :)
Answers.UserDisplayName: Rob Hodges
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-12T18:37:08.333Z
Answers.Text: See https://docs.craftcms.com/v3/element-queries.html
Answers.UserDisplayName: Luke Holder
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-05T11:28:39.540Z
Answers.Text: Just a heads up, the element query page of the craft v3 docs doesn't actually help. If you create an element query and then try to search by a custom field it will absolutely fail as it's expecting it to be a 'real' field. The docs surrounding this in v3 really don't help at all. A simple thing like searching for users by a custom field is seemingly impossible.
Answers.UserDisplayName: Sk446
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-09T14:30:16.700Z
Answers.LastActivityDate: 2015-03-09T14:30:16.700Z
Answers.UserDisplayName: Luke Holder
Body: <p>From within a plugin, I’m trying to filter users by a custom field. </p>
  
  <p>An external post request will supply a variable which I want to match the custom field to. I then want to respond to that request with other user custom fields as JSON. </p>
  
  <p>For example the incoming post contains customHandle1=A, I then find all the users where customHandle1=A and return customHandle2 and customHandle3 data.  </p>
  
  <p>How would I do that?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-03-09T14:04:25.953Z
Comments.Text: OK, just saw your question edit... will answer...
Comments.UserDisplayName: Luke Holder
Tags: <plugin-development><users><plugins>
Title: Filter users by a custom field?
ThreadText: # Filter users by a custom field?
  
  (Asked by: username_0 on 2015-03-09)
  
  From within a plugin, I’m trying to filter users by a custom field.
  
  An external post request will supply a variable which I want to match the custom field to. I then want to respond to that request with other user custom fields as JSON.
  
  For example the incoming post contains customHandle1=A, I then find all the users where customHandle1=A and return customHandle2 and customHandle3 data.
  
  How would I do that?
  
  **Tags:** plugin-development, users, plugins
  
  > **Comments:**
  > **username_1**: OK, just saw your question edit... will answer...  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-03-09. Score: 4 | Accepted answer
  
  To get a user by a custom field, you would need to create a ElementCriteriaModel like this:
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::User);
  $criteria->fieldName = craft()->request->getPost('fieldName');
  
  $users = $criteria->find();
  
  ```
  
  you could then loop over $users and use getAttributes() to get the user data, and if you need the attribute names use attributeNames() if you need the users fields.
  
  > **Comments:**
  > **username_2**: Hi username_1, how would you do this in Craft 3? :)  
  > **username_1**: See https://docs.craftcms.com/v3/element-queries.html  
  > **username_3**: Just a heads up, the element query page of the craft v3 docs doesn't actually help. If you create an element query and then try to search by a custom field it will absolutely fail as it's expecting it to be a 'real' field. The docs surrounding this in v3 really don't help at all. A simple thing like searching for users by a custom field is seemingly impossible.  
  
  ---
  
  