<p>Hopefully, I understood the question properly.</p>

<p>You could start by fetching your <code>subcategories</code> <strong>ids</strong> doing:</p>

<pre class="lang-twig prettyprint-override"><code>{% set subcategories = craft
    .categories
    .group('prodCat')
    .descendantOf(prodCat)
    .level(2)
    .relatedto(entry)
    .ids() %}
</code></pre>

<p>Then query your entries at once rather than looping on your <code>subcategories</code>:</p>

<pre class="lang-twig prettyprint-override"><code>{% set relatedEntries = craft
    .entries
    .section('products')
    .relatedTo({
        targetElement : ['or'] | merge(subcategories) 
    })
    .id('not '~entry.id)
    .all() %}
</code></pre>

<p>So that you only loop your <code>relatedEntries</code> once.</p>

<pre class="lang-twig prettyprint-override"><code>{% if relatedEntries | length %}
    &lt;div&gt;
        &lt;h4&gt;Recommended Product&lt;/h4&gt;
        &lt;ul&gt;
        {% for entry in relatedEntries %}
            &lt;li&gt;{{entry.title}}&lt;/li&gt;
        {% endfor %}
        &lt;/ul&gt;
    &lt;/div&gt;
{% endif %}
</code></pre>

<p>Altogether giving:</p>

<pre class="lang-twig prettyprint-override"><code>{% set subcategories = craft
    .categories()
    .group('prodCat')
    .descendantOf(prodCat)
    .level(2)
    .relatedto(entry)
    .ids() %}

{% if subcategories %}
    {% set relatedEntries = craft
    .entries()
    .section('products')
    .relatedTo({
        targetElement : ['or'] | merge(subcategories) 
    })
    .id('not '~entry.id)
    .all() %}

    {% if relatedEntries %}
        &lt;div&gt;
            &lt;h4&gt;Recommended Product&lt;/h4&gt;
            &lt;ul&gt;
                {% for entry in relatedentry %}
                &lt;li&gt;{{entry.title}}&lt;/li&gt;
                {% endfor %}
            &lt;/ul&gt;
        &lt;/div&gt;
    {% endif %}

{% endif %}
</code></pre>

<p>If you really need to loop the way you are already doing, you could do:</p>

<pre class="lang-twig prettyprint-override"><code>{% if loop.index == 1 %}
&lt;div&gt;
    &lt;h4&gt;Recommended Product&lt;/h4&gt;
    &lt;ul&gt;
{% endif %}
        {% for entry in relatedentry %}
        &lt;li&gt;{{entry.title}}&lt;/li&gt;
        {% endfor %}
{% if loop.last %}
    &lt;/ul&gt;
&lt;/div&gt;
{% endif %}
</code></pre>

<p>Which would only print your header on the first iteration and close the <code>ul</code> and <code>div</code>. It's not as nice but it should do the job!</p>

CC BY-SA 4.0
2020-04-13T03:41:31.443Z
Fetching subcategories ids doesn't work. It didn't show my entry. But the last part 'loop.index' working fine. Thanks.
Lee
CC BY-SA 4.0
2020-04-10T14:06:04.907Z
2020-04-10T14:12:36.137Z
2020-04-10T14:12:36.137Z
Oli
<p>You can collect the id from the previous entry, then you can exclude id in the next query. 
For example like this :</p>

<pre><code> {% set subcategories = craft.categories.group('prodCat').descendantOf(prodCat).level(2).relatedto(entry) %}
 {% set listId = ['not', entry.id ] %} //get id from current page id.
        {% if subcategories | length %}
            {% for subcategory in subcategories %}
                {% set relatedentry = craft.entries.section('products').relatedTo(subcategories).id(listId).all() %}
                {% set relatedentry_count = relatedentry|length %}
                {% if relatedentry_count &gt; 0 %}
                &lt;div&gt;
                    &lt;h4&gt;Recommended Product&lt;/h4&gt;
                    &lt;ul&gt;
                        {% for entry in relatedentry %}
                        {% set listId = listId|merge([entry.id]) %} // append the id from current category to the array.
                        &lt;li&gt;{{entry.title}}&lt;/li&gt;
                        {% endfor %}
                    &lt;/ul&gt;
                &lt;/div&gt;
                {% endif %}
            {% endfor %}
        {% endif %}
</code></pre>

CC BY-SA 4.0
2020-04-13T03:34:14.727Z
This doesn't solve my issue. The title 'Recommended Product' shows twice when there are multiple categories.
Lee
CC BY-SA 4.0
2020-04-10T12:22:12.533Z
2020-04-10T12:22:12.533Z
aodihis
<p>I want to show the entries which have same category of that entry. The entry might have multiple categories, so I want to merge the result. If there is a result, then only the whole structure will be shown. The title 'Recommended Product' should only show one time. Currently, the issue is if there are three categories, the whole structure will show three times.</p>

<pre><code>{% set subcategories = craft.categories.group('prodCat').descendantOf(prodCat).level(2).relatedto(entry) %}
    {% if subcategories | length %}
        {% for subcategory in subcategories %}
            {% set relatedentry = craft.entries.section('products').relatedTo(subcategories).id('not '~entry.id).all() %}
            {% set relatedentry_count = relatedentry|length %}
            {% if relatedentry_count &gt; 0 %}
            &lt;div&gt;
                &lt;h4&gt;Recommended Product&lt;/h4&gt;
                &lt;ul&gt;
                    {% for entry in relatedentry %}
                    &lt;li&gt;{{entry.title}}&lt;/li&gt;
                    {% endfor %}
                &lt;/ul&gt;
            &lt;/div&gt;
            {% endif %}
        {% endfor %}
    {% endif %}
</code></pre>

<p>Update based on the answer:</p>

<p>I manage to solve the issue after making some tweak of the answer given, just merge the subcategory. </p>

<pre><code>{% set subcategory = craft.categories.group('prodCat').level(2).relatedto(entry) %}
{% set subcategories = subcategory|merge(subcategory) %}
    {% if subcategories | length %}
        {% set relatedentry = craft.entries.section('products').relatedTo(subcategories).id('not '~entry.id).all() %}
        {% set relatedentry_count = relatedentry|length %}
        {% if relatedentry_count &gt; 0 %}
            &lt;div&gt;
                &lt;h4&gt;Recommended Product&lt;/h4&gt;
                &lt;ul&gt;
                    {% for entry in relatedentry %}
                    &lt;li&gt;{{entry.title}}&lt;/li&gt;
                    {% endfor %}
                &lt;/ul&gt;
            &lt;/div&gt;
        {% endif %}
    {% endif %}
</code></pre>

<category><relatedto><merge>
Show only and merge the entries from multiple categories related to that entry
# Show only and merge the entries from multiple categories related to that entry

(Asked by: username_0 on 2020-04-10)

I want to show the entries which have same category of that entry. The entry might have multiple categories, so I want to merge the result. If there is a result, then only the whole structure will be shown. The title 'Recommended Product' should only show one time. Currently, the issue is if there are three categories, the whole structure will show three times.
{% set subcategories = craft.categories.group('prodCat').descendantOf(prodCat).level(2).relatedto(entry) %}
    {% if subcategories | length %}
        {% for subcategory in subcategories %}
            {% set relatedentry = craft.entries.section('products').relatedTo(subcategories).id('not '~entry.id).all() %}
            {% set relatedentry_count = relatedentry|length %}
            {% if relatedentry_count > 0 %}
            
                Recommended Product
                
                    {% for entry in relatedentry %}
                    {{entry.title}}
                    {% endfor %}
                
            
            {% endif %}
        {% endfor %}
    {% endif %}

Update based on the answer:
I manage to solve the issue after making some tweak of the answer given, just merge the subcategory. 
{% set subcategory = craft.categories.group('prodCat').level(2).relatedto(entry) %}
{% set subcategories = subcategory|merge(subcategory) %}
    {% if subcategories | length %}
        {% set relatedentry = craft.entries.section('products').relatedTo(subcategories).id('not '~entry.id).all() %}
        {% set relatedentry_count = relatedentry|length %}
        {% if relatedentry_count > 0 %}
            
                Recommended Product
                
                    {% for entry in relatedentry %}
                    {{entry.title}}
                    {% endfor %}
                
            
        {% endif %}
    {% endif %}

**Tags:** category, relatedto, merge

---

## 2 answers

### Answer by username_1 on 2020-04-10. Score: 1 | Accepted answer

Hopefully, I understood the question properly.
You could start by fetching your subcategories ids doing:
{% set subcategories = craft
    .categories
    .group('prodCat')
    .descendantOf(prodCat)
    .level(2)
    .relatedto(entry)
    .ids() %}

Then query your entries at once rather than looping on your subcategories:
{% set relatedEntries = craft
    .entries
    .section('products')
    .relatedTo({
        targetElement : ['or'] | merge(subcategories) 
    })
    .id('not '~entry.id)
    .all() %}

So that you only loop your relatedEntries once.
{% if relatedEntries | length %}
    
        Recommended Product
        
        {% for entry in relatedEntries %}
            {{entry.title}}
        {% endfor %}
        
    
{% endif %}

Altogether giving:
{% set subcategories = craft
    .categories()
    .group('prodCat')
    .descendantOf(prodCat)
    .level(2)
    .relatedto(entry)
    .ids() %}

{% if subcategories %}
    {% set relatedEntries = craft
    .entries()
    .section('products')
    .relatedTo({
        targetElement : ['or'] | merge(subcategories) 
    })
    .id('not '~entry.id)
    .all() %}

    {% if relatedEntries %}
        
            Recommended Product
            
                {% for entry in relatedentry %}
                {{entry.title}}
                {% endfor %}
            
        
    {% endif %}

{% endif %}

If you really need to loop the way you are already doing, you could do:
{% if loop.index == 1 %}

    Recommended Product
    
{% endif %}
        {% for entry in relatedentry %}
        {{entry.title}}
        {% endfor %}
{% if loop.last %}
    

{% endif %}

Which would only print your header on the first iteration and close the ul and div. It's not as nice but it should do the job!

> **Comments:**
> **username_0**: Fetching subcategories ids doesn't work. It didn't show my entry. But the last part 'loop.index' working fine. Thanks.  

---

### Answer by username_2 on 2020-04-10. Score: 1

You can collect the id from the previous entry, then you can exclude id in the next query. 
For example like this :
 {% set subcategories = craft.categories.group('prodCat').descendantOf(prodCat).level(2).relatedto(entry) %}
 {% set listId = ['not', entry.id ] %} //get id from current page id.
        {% if subcategories | length %}
            {% for subcategory in subcategories %}
                {% set relatedentry = craft.entries.section('products').relatedTo(subcategories).id(listId).all() %}
                {% set relatedentry_count = relatedentry|length %}
                {% if relatedentry_count > 0 %}
                
                    Recommended Product
                    
                        {% for entry in relatedentry %}
                        {% set listId = listId|merge([entry.id]) %} // append the id from current category to the array.
                        {{entry.title}}
                        {% endfor %}
                    
                
                {% endif %}
            {% endfor %}
        {% endif %}

> **Comments:**
> **username_0**: This doesn't solve my issue. The title 'Recommended Product' shows twice when there are multiple categories.  

---

