<p>This is untested as I dont have anything set that meets these conditions to try it out in, but I think you can use something like this:</p>

<pre><code>{% set targetElements = ['and'] %}
{% for category in productSpecsCategory %}
    {% set targetElements = targetElements|merge([{ targetElement: category }]) %}
{% endfor %}

{% set entries = craft.commerce.products('footwear').relatedTo(targetElements) %}
</code></pre>

<p>Inspired by this <a href="https://craftcms.stackexchange.com/questions/5107/users-relatedto-multiple-categories">answer</a>.</p>

CC BY-SA 3.0
2017-07-19T15:33:13.947Z
Yep, that is exactly what I was looking for. I tested it on multiple criteria and it works just like my switch (only better of course). Thanks!
Darryl Hardin
CC BY-SA 3.0
2017-07-18T08:42:40.640Z
2017-07-18T08:42:40.640Z
mrsmn
<p>I am still waiting on a better solution, but this is how I've solved this so far. My query will likely never go over 10, but for multiple reasons a dynamic solution would be much better if there is an answer out there.</p>

<pre><code>{% set queryLength = productSpecsCategory|length %}
{% switch queryLength %}
    {% case 1 %}
        {% set entries = craft.commerce.products('footwear').relatedTo([ 'and',
            {targetElement: productSpecsCategory, field: 'productSpecs'}
        ]) %}

    {% case 2 %}
        {% set entries = craft.commerce.products('footwear').relatedTo([ 'and',
            {targetElement: productSpecsCategory[0], field: 'productSpecs'},
            {targetElement: productSpecsCategory[1], field: 'productSpecs'}
        ]) %}
    {% case 3 %}
        {% set entries = craft.commerce.products('footwear').relatedTo([ 'and',
            {targetElement: productSpecsCategory[0], field: 'productSpecs'},
            {targetElement: productSpecsCategory[1], field: 'productSpecs'},
            {targetElement: productSpecsCategory[2], field: 'productSpecs'}
        ]) %}
    {% default %}
{% endswitch %}
</code></pre>

CC BY-SA 3.0
2017-07-16T20:28:56.857Z
2017-07-16T20:28:56.857Z
Darryl Hardin
<p>I have a form that is set up to include multiple values with checkboxes. I pass all those values into an array </p>

<pre><code>{% set specsQuery = craft.request.getParam('specs') %}
{% set productSpecsCategory = craft.categories.group('productSpecs').slug(specsQuery) %}
</code></pre>

<p>My problem is the code below is seeing those values as OR instead of AND. I'm assuming it's because it's all put on the one targetElement line. The amount of criteria a user can select is essentially infinite (as in the max right now is 5 but more categories can be added in craftcms admin area and are created dynamically on the site as more options.</p>

<pre><code>{% set entries = craft.commerce.products('footwear').relatedTo('and',
      { targetElement: productSpecsCategory, field: 'productSpecs' }
) %}
</code></pre>

<p>edit: I have tried</p>

<pre><code>{% set entries = craft.commerce.products('footwear').relatedTo(['and',
  { targetElement: productSpecsCategory, field: 'productSpecs' }
]) %}
</code></pre>

<p>edit: This works with 2 criteria, but no more or less (obviously). Is there a way to loop the targetElement?</p>

<pre><code>{% set entries = craft.commerce.products('footwear').relatedTo([ 'and',
  {targetElement: productSpecsCategory[0], field: 'productSpecs'},
  {targetElement: productSpecsCategory[1], field: 'productSpecs'}
]) %}
</code></pre>

<categories><search><filter><relatedto>
Filtering dynamic criteria with AND
# Filtering dynamic criteria with AND

(Asked by: username_0 on 2017-07-16)

I have a form that is set up to include multiple values with checkboxes. I pass all those values into an array 
{% set specsQuery = craft.request.getParam('specs') %}
{% set productSpecsCategory = craft.categories.group('productSpecs').slug(specsQuery) %}

My problem is the code below is seeing those values as OR instead of AND. I'm assuming it's because it's all put on the one targetElement line. The amount of criteria a user can select is essentially infinite (as in the max right now is 5 but more categories can be added in craftcms admin area and are created dynamically on the site as more options.
{% set entries = craft.commerce.products('footwear').relatedTo('and',
      { targetElement: productSpecsCategory, field: 'productSpecs' }
) %}

edit: I have tried
{% set entries = craft.commerce.products('footwear').relatedTo(['and',
  { targetElement: productSpecsCategory, field: 'productSpecs' }
]) %}

edit: This works with 2 criteria, but no more or less (obviously). Is there a way to loop the targetElement?
{% set entries = craft.commerce.products('footwear').relatedTo([ 'and',
  {targetElement: productSpecsCategory[0], field: 'productSpecs'},
  {targetElement: productSpecsCategory[1], field: 'productSpecs'}
]) %}

**Tags:** categories, search, filter, relatedto

---

## 2 answers

### Answer by username_1 on 2017-07-18. Score: 2 | Accepted answer

This is untested as I dont have anything set that meets these conditions to try it out in, but I think you can use something like this:
{% set targetElements = ['and'] %}
{% for category in productSpecsCategory %}
    {% set targetElements = targetElements|merge([{ targetElement: category }]) %}
{% endfor %}

{% set entries = craft.commerce.products('footwear').relatedTo(targetElements) %}

Inspired by this answer.

> **Comments:**
> **username_0**: Yep, that is exactly what I was looking for. I tested it on multiple criteria and it works just like my switch (only better of course). Thanks!  

---

### Answer by username_0 on 2017-07-16. Score: 1

I am still waiting on a better solution, but this is how I've solved this so far. My query will likely never go over 10, but for multiple reasons a dynamic solution would be much better if there is an answer out there.
{% set queryLength = productSpecsCategory|length %}
{% switch queryLength %}
    {% case 1 %}
        {% set entries = craft.commerce.products('footwear').relatedTo([ 'and',
            {targetElement: productSpecsCategory, field: 'productSpecs'}
        ]) %}

    {% case 2 %}
        {% set entries = craft.commerce.products('footwear').relatedTo([ 'and',
            {targetElement: productSpecsCategory[0], field: 'productSpecs'},
            {targetElement: productSpecsCategory[1], field: 'productSpecs'}
        ]) %}
    {% case 3 %}
        {% set entries = craft.commerce.products('footwear').relatedTo([ 'and',
            {targetElement: productSpecsCategory[0], field: 'productSpecs'},
            {targetElement: productSpecsCategory[1], field: 'productSpecs'},
            {targetElement: productSpecsCategory[2], field: 'productSpecs'}
        ]) %}
    {% default %}
{% endswitch %}


---

