<p>Does something like this work? You might need to add some wildcards as <a href="http://buildwithcraft.com/docs/searching" rel="nofollow">documented here</a> to run the right kind of search.</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
$users = $criteria-&gt;search('customField: *' . $someValue.'*');
</code></pre>

CC BY-SA 3.0
2015-04-14T13:49:11.817Z
Thanks Aaron. I should have mentioned that I’m using search as I need to find a value within that field in this case. Field is typically 1,2,3 and I need to check for on of those values.
James
CC BY-SA 3.0
2015-04-14T13:56:02.517Z
Hey James. Thanks for the clarification! Does the updated answer help?
Aaron Berkowitz
CC BY-SA 3.0
2015-04-14T15:02:02.483Z
That doesn’t work unfortunately. Without the wildcard I’m getting some results just not all of them it seems.
James
CC BY-SA 3.0
2015-04-14T15:07:26.130Z
Hmm ok. Can you provide some details about the field? Is it a plain text field? What kind of values are typically stored and what kind of example values are you searching for?
Aaron Berkowitz
CC BY-SA 3.0
2015-04-14T15:57:29.387Z
I am actually looking for a number value in a string. So, literally $someValue = 1 and the user custom field is plain text with 1,2,3. I’ve just realised the users that get missed are those that had those fields populated by another plugin. This looks as though the data is not being saved in the same way as when it’s entered manually in the CP.
James
CC BY-SA 3.0
2015-04-14T16:02:45.580Z
Yep, existing fields that contain 1,2,3 are not returned. When I replace this in the CP manually with the same 1,2,3, they show up.
James
CC BY-SA 3.0
2015-04-14T13:45:14.343Z
2015-04-14T13:55:55.427Z
2015-04-14T13:55:55.427Z
Aaron Berkowitz
<p>This did work until there’s a significant amount of users: </p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
$users = $criteria-&gt;search('customField:' . $someValue);
</code></pre>

<p>I’m using search as I need to find a value within the custom field. The field is typically 1,2,3 and I need to check for one of those values.</p>

<p>I have also tried to limit it without success.</p>

<pre><code>$criteria-&gt;groupId = 1;
$criteria-&gt;limit = null;
</code></pre>

<p>What’s the best way to do this?</p>

<p>UPDATE:</p>

<p>The users getting missed are those that had the fields I’m searching populated by another plugin. This looks as though the data is not being saved in the same way as when it’s entered manually in the CP.</p>

CC BY-SA 3.0
2015-04-14T13:15:24.800Z
Just curious, what's a significant amount of users?
Peter Tell
CC BY-SA 3.0
2015-04-14T13:35:18.437Z
Not many actually, just over 200 in this case.
James
<plugin-development><users><elementcriteriamodel>
Best way to search users by a custom field inside a plugin?
# Best way to search users by a custom field inside a plugin?

(Asked by: username_0 on 2015-04-14)

This did work until there’s a significant amount of users: 
$criteria = craft()->elements->getCriteria(ElementType::User);
$users = $criteria->search('customField:' . $someValue);

I’m using search as I need to find a value within the custom field. The field is typically 1,2,3 and I need to check for one of those values.
I have also tried to limit it without success.
$criteria->groupId = 1;
$criteria->limit = null;

What’s the best way to do this?
UPDATE:
The users getting missed are those that had the fields I’m searching populated by another plugin. This looks as though the data is not being saved in the same way as when it’s entered manually in the CP.

**Tags:** plugin-development, users, elementcriteriamodel
> **Comments:**
> **username_1**: Just curious, what's a significant amount of users?  
> **username_0**: Not many actually, just over 200 in this case.  

---

## 1 answers

### Answer by username_2 on 2015-04-14. Score: 1

Does something like this work? You might need to add some wildcards as documented here to run the right kind of search.
$criteria = craft()->elements->getCriteria(ElementType::User);
$users = $criteria->search('customField: *' . $someValue.'*');

> **Comments:**
> **username_0**: Thanks Aaron. I should have mentioned that I’m using search as I need to find a value within that field in this case. Field is typically 1,2,3 and I need to check for on of those values.  
> **username_2**: Hey James. Thanks for the clarification! Does the updated answer help?  
> **username_0**: That doesn’t work unfortunately. Without the wildcard I’m getting some results just not all of them it seems.  
> **username_2**: Hmm ok. Can you provide some details about the field? Is it a plain text field? What kind of values are typically stored and what kind of example values are you searching for?  
> **username_0**: I am actually looking for a number value in a string. So, literally $someValue = 1 and the user custom field is plain text with 1,2,3. I’ve just realised the users that get missed are those that had those fields populated by another plugin. This looks as though the data is not being saved in the same way as when it’s entered manually in the CP.  
> **username_0**: Yep, existing fields that contain 1,2,3 are not returned. When I replace this in the CP manually with the same 1,2,3, they show up.  

---

