How to return entries based on Checkboxes field criteria? Ok I'm building a plugin that needs to check for an id in an array and return only users that have the id in the checkbox array.

So this is what I have:

$user_criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
$user_criteria-&gt;interests = $interestId;
$users = $user_criteria-&gt;find();


interests is a dynamically created checkbox array which sits under the users profile. $interestId is the id I wish to look for in that array and return all users that have it.

Any help much appreciated.
The default Checkboxes field type stores its value to the database as a serialized PHP array. So for an entry with checkboxes "7", "12" and "32" checked, the value in the field_interests column is ["7","12","32"].

This is not query friendly at all, so the best solution would be to use a different field type.

But if you really want to use the checkboxes field, here's how you can query for your elements. Convert your criteria model to a DbCommand object and add a new WHERE clause with a LIKE condition to perform pattern matching. Include the quotes enclosing each ID in the pattern, this should make the query robust.

$user_criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);

$query = craft()-&gt;elements-&gt;buildElementsQuery($user_criteria);
$params = array(
    ':interestId' =&gt; '%"'.$interestId.'"%',
);
$query-&gt;andWhere('field_interests LIKE :interestId', $params);
$results = $query-&gt;queryAll();

$users = EntryModel::populateModels($results);