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?

  • OK, just saw your question edit... will answer... Commented Mar 9, 2015 at 14:04

1 Answer 1


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.

  • Hi Luke, how would you do this in Craft 3? :)
    – Rob Hodges
    Commented Apr 12, 2018 at 14:49
  • See docs.craftcms.com/v3/element-queries.html Commented Apr 12, 2018 at 18:37
  • 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.
    – Sk446
    Commented Jan 5, 2021 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.