<p>This line doesn't return an array of items; it returns an ElementCriteriaModel:</p>

<pre><code>{% set entries = craft.entries.relatedTo(category) %}
</code></pre>

<p>That's probably why you're having problems with the <code>|merge</code>.</p>

<p>Changing it to the following should do the trick:</p>

<pre><code>{% set entries = craft.entries.relatedTo(category).find() %}
</code></pre>

CC BY-SA 3.0
2017-03-06T23:59:57.367Z
2017-03-06T23:59:57.367Z
Stephen Lewis
<p>In the grand scheme, I am trying to get all products that are <em>not</em> related to specific categories.</p>

<p>Since any product can belong to any given number of categories, I am trying to loop through all categories and then only query against the categories that I want.</p>

<p>This is what I have so far:</p>

<pre><code>{% set excludeCategoryIds = [36, 44] %}
{% set allProducts = craft.commerce.products.type('myType').order('title asc') %}
{% set products = [] %}

{% for category in craft.categories.group('myCategories').relatedTo(allProducts) %}
    {% if category.id not in excludeCategoryIds %}
        {% set entries = craft.entries.relatedTo(category) %}

        {# push entires into products?? #}
        {% set products = products|merge([entries]) %} {# doesn't work #}
    {% endif %}
{% endfor %}
</code></pre>

<p>Once all products are found, then I'll loop through them.</p>

<pre><code>{% if products|length %}
    ...
{% endif %}
</code></pre>

<p>I could also loop through each of the entires as <a href="https://craftcms.com/support/entries-related-to-category" rel="nofollow noreferrer">described in the docs</a> but it's starting to feel like this is getting out of control.</p>

<p>Is there a better way to only show products that are not related to specific categories?</p>

<p>Thank you for any suggestions!</p>

<templating>
How can I push a result of entries/products from a loop into an array?
# How can I push a result of entries/products from a loop into an array?

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

In the grand scheme, I am trying to get all products that are not related to specific categories.
Since any product can belong to any given number of categories, I am trying to loop through all categories and then only query against the categories that I want.
This is what I have so far:
{% set excludeCategoryIds = [36, 44] %}
{% set allProducts = craft.commerce.products.type('myType').order('title asc') %}
{% set products = [] %}

{% for category in craft.categories.group('myCategories').relatedTo(allProducts) %}
    {% if category.id not in excludeCategoryIds %}
        {% set entries = craft.entries.relatedTo(category) %}

        {# push entires into products?? #}
        {% set products = products|merge([entries]) %} {# doesn't work #}
    {% endif %}
{% endfor %}

Once all products are found, then I'll loop through them.
{% if products|length %}
    ...
{% endif %}

I could also loop through each of the entires as described in the docs but it's starting to feel like this is getting out of control.
Is there a better way to only show products that are not related to specific categories?
Thank you for any suggestions!

**Tags:** templating

---

## 1 answers

### Answer by username_1 on 2017-03-06. Score: 1 | Accepted answer

This line doesn't return an array of items; it returns an ElementCriteriaModel:
{% set entries = craft.entries.relatedTo(category) %}

That's probably why you're having problems with the |merge.
Changing it to the following should do the trick:
{% set entries = craft.entries.relatedTo(category).find() %}


---

