<p>Checkboxes / multiselect fields store to the database in a very query unfriendly way.
That's why I would really recommend to use categories, tags, lightswitches instead, if you intend to
query against these fields' values.</p>

<p>Here's what you'd have to do to search for entries with selected checkboxes field types' options. You basically have to get all relevant entries first,
go through them to check each checkbox option and store the element ID in case the entry is valid. Using this approach you end
up with an array of IDs which you can now query against.</p>

<pre><code>{% set entries = craft.entries.section('news') %}

{% set query = craft.request.getQuery() %}

{% if query.mobilityOptions is defined %}
    {% set ids = [] %}

    {% for entry in entries %}
        {% set valid = true %}

        {% for option in query.mobilityOptions %}
            {% if not entry.mobilityOptions.contains(option) %}
                {% set valid = false %}
            {% endif %}
        {% endfor %}

        {% if valid %}
            {% set ids = ids|merge([entry.id]) %}
        {% endif %}
    {% endfor %}

    {% set entries = craft.entries.id(ids) %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-01-13T13:00:21.247Z
Is there a way to add entities to a category when a field is checked/unchecked via the admin panel? If so, then that would definitely be the best option. If not, then I can't really add categories based on what I am looking for simply because it adds more workload for the person updating the content. I'm going to try the solution you posted to see if it gets what I need, but I have done some filtering based on categories and it was a lot cleaner and intuitive than this.
Nexcapto
CC BY-SA 3.0
2016-01-13T13:53:17.323Z
I'm not sure what you mean with "add entities to a category when a field is checked/unchecked"?
carlcs
CC BY-SA 3.0
2016-01-13T14:00:07.427Z
When someone is in the admin panel, and say they add a product. When an entry field for mobilityOptions is checked, say they check "Cordless", is there a way to automatically add them to a category called "Cordless Phones" instead of them clicking cordless, as well as adding it to that category? Also thanks for the help and follow-up! I'm still not quite there on the solution though..
Nexcapto
CC BY-SA 3.0
2016-01-13T14:13:02.187Z
"When an entry field for mobilityOptions is checked" – do you mean an [Entries field](https://craftcms.com/docs/entries-fields)? I was actually suggesting to replace the checkboxes field with an Categories field that you would link up to a category group "Mobility Options". Sorry, but I guess I miss something.
carlcs
CC BY-SA 3.0
2016-01-13T14:44:24.157Z
I'm with you. I understand you suggested I use categories instead of the fields. But what I am asking is, because I am working on a live site with a lot of entries, is it possible to sweep through them all, and place them in a category based on that field so that when I do set up the search by category, it would actually find the results. Does that explain it better? I'm looking for a way to add products to categories based on field selections when creating them. They still need those fields for when you are on the products page.
Nexcapto
CC BY-SA 3.0
2016-01-13T15:05:44.907Z
Gotcha, you want to keep the checkboxes field (for the clients' sake) but want to copy over the selection to a Cartegories field on every entry save. This is possible with a custom plugin that listens to `onSaveEntry` events, loops the checkboxes options and stores the relations to the new field (which you could hide with CSS from the CP). You'd just have to make sure to have a matching category for each checkbox option and find a way to link them (e.g., category slug matches checkbox option value).
carlcs
CC BY-SA 3.0
2016-01-13T11:36:15.770Z
2016-01-13T11:36:15.770Z
carlcs
<p>Have a look at the <a href="https://github.com/carlcs/craft-checkboxfield" rel="nofollow">Checkbox field</a> plugin.</p>

<p>It provides a field type that looks like the default <a href="https://craftcms.com/docs/checkboxes-fields" rel="nofollow">Checkboxes field</a>, but stores a boolean value to the database (similar to the Lightswitch field), so you can use a custom field parameter in your criteria model.</p>

<pre><code>{% set entries = craft.entries.myCheckbox(1).myOtherCheckbox(0) %}
</code></pre>

CC BY-SA 3.0
2016-01-18T15:26:37.807Z
I marked your other answer as it got me what I needed. Thanks!
Nexcapto
CC BY-SA 3.0
2016-01-16T18:14:52.833Z
2016-01-16T18:14:52.833Z
carlcs
<p>I have a relatively simple structure for products that I am building an advanced search for. I access the products using <code>craft.entries.section('products')</code>, but these products have fields with options for those fields within them.</p>

<p>So my question is, how do I access these? This is my attempt so far.</p>

<pre><code>        {% set parameters=
            {
                section: 'products',
                limit: '1'
            }
        %}

        {% set query = craft.request.getParam('entry') %}

        {% if query %}
            {% set entries = craft.entries(parameters).relatedTo(query).order('score') %}
        {% else %}
            {% set entries = craft.entries(parameters) %}
        {% endif %}
</code></pre>

<p>I know this is incorrect, because I need to look at an object within an object within products and check a field belonging it. For instance, if the product was a phone, it would have something like "mobilityOptions" and under that would be "Cordless", "Desk", and "Wall". </p>

<p>My search form has got those mobility options through a for option in products loop of the section, followed by a for block in <code>option.mobilityOptions.options</code> that then makes checkboxes for each option, but to use it with the ".relatedTo" functionality I don't think it would work.</p>

<entries><search><checkboxes>
Search against a checkboxes field's options
# Search against a checkboxes field's options

(Asked by: username_0 on 2016-01-12)

I have a relatively simple structure for products that I am building an advanced search for. I access the products using craft.entries.section('products'), but these products have fields with options for those fields within them.
So my question is, how do I access these? This is my attempt so far.
        {% set parameters=
            {
                section: 'products',
                limit: '1'
            }
        %}

        {% set query = craft.request.getParam('entry') %}

        {% if query %}
            {% set entries = craft.entries(parameters).relatedTo(query).order('score') %}
        {% else %}
            {% set entries = craft.entries(parameters) %}
        {% endif %}

I know this is incorrect, because I need to look at an object within an object within products and check a field belonging it. For instance, if the product was a phone, it would have something like "mobilityOptions" and under that would be "Cordless", "Desk", and "Wall". 
My search form has got those mobility options through a for option in products loop of the section, followed by a for block in option.mobilityOptions.options that then makes checkboxes for each option, but to use it with the ".relatedTo" functionality I don't think it would work.

**Tags:** entries, search, checkboxes


---

## 2 answers

### Answer by username_1 on 2016-01-13. Score: 5 | Accepted answer

Checkboxes / multiselect fields store to the database in a very query unfriendly way.
That's why I would really recommend to use categories, tags, lightswitches instead, if you intend to
query against these fields' values.
Here's what you'd have to do to search for entries with selected checkboxes field types' options. You basically have to get all relevant entries first,
go through them to check each checkbox option and store the element ID in case the entry is valid. Using this approach you end
up with an array of IDs which you can now query against.
{% set entries = craft.entries.section('news') %}

{% set query = craft.request.getQuery() %}

{% if query.mobilityOptions is defined %}
    {% set ids = [] %}

    {% for entry in entries %}
        {% set valid = true %}

        {% for option in query.mobilityOptions %}
            {% if not entry.mobilityOptions.contains(option) %}
                {% set valid = false %}
            {% endif %}
        {% endfor %}

        {% if valid %}
            {% set ids = ids|merge([entry.id]) %}
        {% endif %}
    {% endfor %}

    {% set entries = craft.entries.id(ids) %}
{% endif %}

> **Comments:**
> **username_0**: Is there a way to add entities to a category when a field is checked/unchecked via the admin panel? If so, then that would definitely be the best option. If not, then I can't really add categories based on what I am looking for simply because it adds more workload for the person updating the content. I'm going to try the solution you posted to see if it gets what I need, but I have done some filtering based on categories and it was a lot cleaner and intuitive than this.  
> **username_1**: I'm not sure what you mean with "add entities to a category when a field is checked/unchecked"?  
> **username_0**: When someone is in the admin panel, and say they add a product. When an entry field for mobilityOptions is checked, say they check "Cordless", is there a way to automatically add them to a category called "Cordless Phones" instead of them clicking cordless, as well as adding it to that category? Also thanks for the help and follow-up! I'm still not quite there on the solution though..  
> **username_1**: "When an entry field for mobilityOptions is checked" – do you mean an [Entries field](https://craftcms.com/docs/entries-fields)? I was actually suggesting to replace the checkboxes field with an Categories field that you would link up to a category group "Mobility Options". Sorry, but I guess I miss something.  
> **username_0**: I'm with you. I understand you suggested I use categories instead of the fields. But what I am asking is, because I am working on a live site with a lot of entries, is it possible to sweep through them all, and place them in a category based on that field so that when I do set up the search by category, it would actually find the results. Does that explain it better? I'm looking for a way to add products to categories based on field selections when creating them. They still need those fields for when you are on the products page.  
> **username_1**: Gotcha, you want to keep the checkboxes field (for the clients' sake) but want to copy over the selection to a Cartegories field on every entry save. This is possible with a custom plugin that listens to `onSaveEntry` events, loops the checkboxes options and stores the relations to the new field (which you could hide with CSS from the CP). You'd just have to make sure to have a matching category for each checkbox option and find a way to link them (e.g., category slug matches checkbox option value).  

---

### Answer by username_1 on 2016-01-16. Score: 2

Have a look at the Checkbox field plugin.
It provides a field type that looks like the default Checkboxes field, but stores a boolean value to the database (similar to the Lightswitch field), so you can use a custom field parameter in your criteria model.
{% set entries = craft.entries.myCheckbox(1).myOtherCheckbox(0) %}

> **Comments:**
> **username_0**: I marked your other answer as it got me what I needed. Thanks!  

---

