<p>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 <code>field_interests</code> column is <code>["7","12","32"]</code>.</p>

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

<p>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 <a href="http://buildwithcraft.com/classreference/etc/db/DbCommand" rel="nofollow">DbCommand</a> object and add a new <code>WHERE</code> clause with a <a href="https://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html" rel="nofollow"><code>LIKE</code></a> condition to perform pattern matching. Include the quotes enclosing each ID in the pattern, this should make the query robust.</p>

<pre><code>$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);
</code></pre>

CC BY-SA 3.0
2015-10-22T10:37:42.313Z
2015-10-22T10:37:42.313Z
carlcs
<p>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.</p>

<p>So this is what I have:</p>

<pre><code>$user_criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
$user_criteria-&gt;interests = $interestId;
$users = $user_criteria-&gt;find();
</code></pre>

<p>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.</p>

<p>Any help much appreciated.</p>

<plugin-development><users><elementcriteriamodel>
How to return entries based on Checkboxes field criteria?
# How to return entries based on Checkboxes field criteria?

(Asked by: username_0 on 2015-10-20)

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()->elements->getCriteria(ElementType::User);
$user_criteria->interests = $interestId;
$users = $user_criteria->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.

**Tags:** plugin-development, users, elementcriteriamodel


---

## 1 answers

### Answer by username_1 on 2015-10-22. Score: 1

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()->elements->getCriteria(ElementType::User);

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

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


---

