<p>You can limit to only products that have multiple related categories like so:</p>



<pre><code>{% set dressesCat = craft.categories...first() %}
{% set blackCat = craft.categories...first() %}

{% set products = craft.commerce.products({
    relatedTo: {
        targetElement: ['and', dressesCat, blackCat],
        field: 'productCategories'
    }
} %}
</code></pre>

CC BY-SA 3.0
2017-06-15T22:18:43.527Z
2017-06-15T22:18:43.527Z
Brandon Kelly
<p>I've got categories setup like this (Parent &amp; Children):</p>

<pre><code>Type
    Tops
    Dresses
    Pants

Colour
    Black
    Red
    Yellow
</code></pre>

<p>When I use this in a category filter and choose Dresses and Black, it works fine in terms that it will return entries that are in Dresses and entries that are in Black.</p>

<p>But how can I edit my code so that it returns entries that are ONLY in Dresses and Black (Black Dresses only!)</p>

<p>Below is what I have so far...</p>

<pre><code>{% set productSegmentCategory = craft.categories.slug(craft.request.lastSegment()).first() %}
{% set productCategoriesArray = ['and'] %}

{% set productValues = {
    keywords: craft.request.getParam('keywords'),
    category: craft.request.getParam('category') ? craft.categories.slug(craft.request.getParam('category')) : null,
    order: craft.request.getParam('order') ? craft.request.getParam('order') : 'title asc',
    limit: craft.request.getParam('limit') ? craft.request.getParam('limit') : entryLimit,
} %}

{% set productParameters = {
    search: productValues.keywords,
    order: productValues.order,
    limit: null,
}%}

{% if productSegmentCategory|length %}
    {% set productCategoriesArray = productCategoriesArray|merge([{ targetElement: productSegmentCategory, field: 'productCategories' }]) %}
{% endif %}

{% if productValues.category|length %}
    {% set productCategoriesArray = productCategoriesArray|merge([{ targetElement: productValues.category, field: 'productCategories' }]) %}
{% endif %}

{% if productCategoriesArray|length &gt; 1 %}
  {% set productParameters = productParameters|merge({ relatedTo: productCategoriesArray }) %}
{% endif %}

{% set productEntries = craft.commerce.products(productParameters) %}

{% if productSegmentCategory|length %}
    {% set entry = productSegmentCategory %}
{% endif %}
</code></pre>

<categories><search><filter><category>
Entries related to BOTH categories
# Entries related to BOTH categories

(Asked by: username_0 on 2017-05-05)

I've got categories setup like this (Parent & Children):
Type
    Tops
    Dresses
    Pants

Colour
    Black
    Red
    Yellow

When I use this in a category filter and choose Dresses and Black, it works fine in terms that it will return entries that are in Dresses and entries that are in Black.
But how can I edit my code so that it returns entries that are ONLY in Dresses and Black (Black Dresses only!)
Below is what I have so far...
{% set productSegmentCategory = craft.categories.slug(craft.request.lastSegment()).first() %}
{% set productCategoriesArray = ['and'] %}

{% set productValues = {
    keywords: craft.request.getParam('keywords'),
    category: craft.request.getParam('category') ? craft.categories.slug(craft.request.getParam('category')) : null,
    order: craft.request.getParam('order') ? craft.request.getParam('order') : 'title asc',
    limit: craft.request.getParam('limit') ? craft.request.getParam('limit') : entryLimit,
} %}

{% set productParameters = {
    search: productValues.keywords,
    order: productValues.order,
    limit: null,
}%}

{% if productSegmentCategory|length %}
    {% set productCategoriesArray = productCategoriesArray|merge([{ targetElement: productSegmentCategory, field: 'productCategories' }]) %}
{% endif %}

{% if productValues.category|length %}
    {% set productCategoriesArray = productCategoriesArray|merge([{ targetElement: productValues.category, field: 'productCategories' }]) %}
{% endif %}

{% if productCategoriesArray|length > 1 %}
  {% set productParameters = productParameters|merge({ relatedTo: productCategoriesArray }) %}
{% endif %}

{% set productEntries = craft.commerce.products(productParameters) %}

{% if productSegmentCategory|length %}
    {% set entry = productSegmentCategory %}
{% endif %}

**Tags:** categories, search, filter, category


---

## 1 answers

### Answer by username_1 on 2017-06-15. Score: 2 | Accepted answer

You can limit to only products that have multiple related categories like so:
{% set dressesCat = craft.categories...first() %}
{% set blackCat = craft.categories...first() %}

{% set products = craft.commerce.products({
    relatedTo: {
        targetElement: ['and', dressesCat, blackCat],
        field: 'productCategories'
    }
} %}


---

