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:
<form>
<!-- brand matches dropdown fields-->
<label>
<input type="checkbox" name="brand[]" value ="brandX" />
Brand X
</label>
<label>
<input type="checkbox" name="brand[]" value ="brandY" />
Brand Y
</label>
<!-- location matches checkbox fields -->
<label>
<input type="checkbox" name="location[]" value ="locationA" />
Location A
</label>
<label>
<input type="checkbox" name="location[]" value ="locationB" />
Location B
</label>
</form>
relatedTo
param only works with certain field types and elements. Check out the documentation for more info! – Aaron Berkowitz Oct 20 '15 at 0:52location
? If it isn't a category, entry, asset, or user, it wont work withrelatedTo
. You might need to use search instead. – Aaron Berkowitz Oct 20 '15 at 13:14