<p>I'm probably late to answer this, but I've recentry encountered same problem and here's the solution I came to:</p>

<p>use params for dropdown fields and search for checkbox fields</p>

<p>So my code looks similar to this:</p>

<pre><code>{% set orderParams = {} %}

{% for key, val in craft.request.query %}
    {% if val is not empty %}
        {% if key=='orderFinance' %}
            {% set orderParams = orderParams|merge({orderFinance:val}) %}
        {% elseif key=='orderStatus' %}
            {% set orderParams = orderParams|merge({orderStatus:val}) %}
        {% endif %}
    {% endif %}
{% endfor %}

{% set flagSearchQuery = [] %}
{% for flag in craft.request.query.orderFlag %}
    {% if flag is not empty %}
        {% set flagSearchQuery = flagSearchQuery|merge({query: 'orderFlag:'~flag}) %}
    {% endif %}
{% endfor %}

{% set orders = craft.commerce.orders(orderParams).search(flagSearchQuery).limit(100) %}
</code></pre>

CC BY-SA 3.0
2016-10-27T17:54:39.327Z
2016-10-27T17:54:39.327Z
Yuri Salimovskiy
<p>I have a front end form that allows users check checkboxes to find entries that have matching field values. I've pieced together a query reading other posts here and my limited knowledge of Craft and Twig.</p>

<p>My entries have multiple dropdown fields that are getting matched (1 match per field) and one checkbox field (1 or more matches per field). My query works when I only have dropdown fields but I'm struggling getting it to work to match the checkbox fields.</p>

<p>Here's my paired down query:</p>

<pre><code>    {% set params = {
    section: 'entriesSection',
    limit: null
    } %}

    {% set relatedParams = ['and'] %}

 {# dropdown field match #}
    {% set brand = craft.request.getParam('brand') %}
       {% if getBrand %}
           {% set params = params|merge({'brand':getBrand}) %}
       {% endif %}

 {# checkbox field match #}
    {% set getLocation = craft.request.getParam('location') %}
    {% if getLocation %}
          {% for eachLocation in getLocation %}
              {% set params = params|merge({'eachLocation'}):getLocation}) %}
          {% endfor %}
    {% endif %}

 {# merge related params and params #}
    {% if relatedParams|length &gt; 1 %}
         {% set params = params|merge({'relatedTo':relatedParams}) %}
    {% endif %}

 {# search #}
    {% set entries = craft.entries(params).search(searchParams|trim) %}
</code></pre>

<p>Here's my paired down front-end form:</p>

<pre><code> &lt;form&gt;

&lt;!-- brand matches dropdown fields--&gt;

    &lt;label&gt;
       &lt;input type="checkbox" name="brand[]" value ="brandX" /&gt;
    Brand X
    &lt;/label&gt;
    &lt;label&gt;
       &lt;input type="checkbox" name="brand[]" value ="brandY" /&gt;
    Brand Y
    &lt;/label&gt;

 &lt;!-- location matches checkbox fields --&gt;

    &lt;label&gt;
       &lt;input type="checkbox" name="location[]" value ="locationA" /&gt;
    Location A
    &lt;/label&gt;

    &lt;label&gt;
       &lt;input type="checkbox" name="location[]" value ="locationB" /&gt;
    Location B
    &lt;/label&gt;
 &lt;/form&gt;
</code></pre>

CC BY-SA 3.0
2015-10-20T00:52:53.523Z
For the fields that you want to be 'matched', what kind of field type are they? The `relatedTo` param only works with certain field types and elements. Check out [the documentation](http://buildwithcraft.com/docs/relations) for more info!
Aaron Berkowitz
CC BY-SA 3.0
2015-10-20T13:09:45.227Z
The field type I am having a problem with is a checkboxes field. The other field type is a dropdown and that works ok with the query in my question.
triplethreat23
CC BY-SA 3.0
2015-10-20T13:14:33.437Z
What kind of element is `location`? If it isn't a category, entry, asset, or user, it wont work with `relatedTo`. You might need to use search instead.
Aaron Berkowitz
CC BY-SA 3.0
2015-10-20T15:48:49.790Z
"Location" is a field associated with an entry. "Brand" is the same just a different field type and that works with this query/relatedto as far as I can tell.  Sorry I am really new to this and don't fully understand. According to the documentation, relatedto does work with a field.
triplethreat23
<search><filter><drop-down><checkboxes>
Craft Request Filter Query Matching Drop Down Fields and Checkbox Fields
# Craft Request Filter Query Matching Drop Down Fields and Checkbox Fields

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

I have a front end form that allows users check checkboxes to find entries that have matching field values. I've pieced together a query reading other posts here and my limited knowledge of Craft and Twig.
My entries have multiple dropdown fields that are getting matched (1 match per field) and one checkbox field (1 or more matches per field). My query works when I only have dropdown fields but I'm struggling getting it to work to match the checkbox fields.
Here's my paired down query:
    {% set params = {
    section: 'entriesSection',
    limit: null
    } %}

    {% set relatedParams = ['and'] %}

 {# dropdown field match #}
    {% set brand = craft.request.getParam('brand') %}
       {% if getBrand %}
           {% set params = params|merge({'brand':getBrand}) %}
       {% endif %}

 {# checkbox field match #}
    {% set getLocation = craft.request.getParam('location') %}
    {% if getLocation %}
          {% for eachLocation in getLocation %}
              {% set params = params|merge({'eachLocation'}):getLocation}) %}
          {% endfor %}
    {% endif %}

 {# merge related params and params #}
    {% if relatedParams|length > 1 %}
         {% set params = params|merge({'relatedTo':relatedParams}) %}
    {% endif %}

 {# search #}
    {% set entries = craft.entries(params).search(searchParams|trim) %}

Here's my paired down front-end form:
 



    
       
    Brand X
    
    
       
    Brand Y
    

 

    
       
    Location A
    

    
       
    Location B

**Tags:** search, filter, drop-down, checkboxes

> **Comments:**
> **username_1**: For the fields that you want to be 'matched', what kind of field type are they? The `relatedTo` param only works with certain field types and elements. Check out [the documentation](http://buildwithcraft.com/docs/relations) for more info!  
> **username_0**: The field type I am having a problem with is a checkboxes field. The other field type is a dropdown and that works ok with the query in my question.  
> **username_1**: What kind of element is `location`? If it isn't a category, entry, asset, or user, it wont work with `relatedTo`. You might need to use search instead.  
> **username_0**: "Location" is a field associated with an entry. "Brand" is the same just a different field type and that works with this query/relatedto as far as I can tell.  Sorry I am really new to this and don't fully understand. According to the documentation, relatedto does work with a field.  

---

## 1 answers

### Answer by username_2 on 2016-10-27. Score: 1

I'm probably late to answer this, but I've recentry encountered same problem and here's the solution I came to:
use params for dropdown fields and search for checkbox fields
So my code looks similar to this:
{% set orderParams = {} %}

{% for key, val in craft.request.query %}
    {% if val is not empty %}
        {% if key=='orderFinance' %}
            {% set orderParams = orderParams|merge({orderFinance:val}) %}
        {% elseif key=='orderStatus' %}
            {% set orderParams = orderParams|merge({orderStatus:val}) %}
        {% endif %}
    {% endif %}
{% endfor %}

{% set flagSearchQuery = [] %}
{% for flag in craft.request.query.orderFlag %}
    {% if flag is not empty %}
        {% set flagSearchQuery = flagSearchQuery|merge({query: 'orderFlag:'~flag}) %}
    {% endif %}
{% endfor %}

{% set orders = craft.commerce.orders(orderParams).search(flagSearchQuery).limit(100) %}


---

