<p>Instead of using the Merge filter, I tried &quot;<a href="https://craftcms.com/docs/3.x/relations.html#passing-multiple-relation-criteria" rel="nofollow noreferrer">Passing Multiple Relation Criteria</a>&quot; from the Craft Docs, replacing &quot;and&quot; with &quot;or&quot;, below...</p>
<pre><code>{# get all sticky Project entries #}
{% set stickyProjects = craft.entries().section('project').limit(30).sticky('1') %}
{% set stickyProjectEntries = stickyProjects.all() %}   

{# get all non sticky Project entries #}
{% set projects = craft.entries().section('project').limit(30).sticky('0') %}
{% set projectEntries = projects.all() %}   
        
{# Fetch, then merge, categories related to both sticky and non-sticky Projects #}
{% set stickyCategories = craft.categories()
    .relatedTo([
    'or',
    { sourceElement: stickyProjectEntries },
    { sourceElement: projectEntries },
    ])
    .all() %}

{% set usedCategories = stickyCategories %}
</code></pre>
<p>Which produced the desired result: (image)
<a href="https://i.sstatic.net/wMTAz.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/wMTAz.png" alt="enter image description here" /></a></p>
<p>Thanks all, I hope that helps you all.</p>
<p>:)</p>

CC BY-SA 4.0
2020-11-02T18:59:52.963Z
2020-11-02T18:59:52.963Z
Matt P
<p>good to be back)</p>
<p>Helping a friend out with a website for his tiling business. The site contains a 'projects' section which displays category-filter buttons allowing the user to toggle relevant projects.</p>
<p>The CMS also contains a custom field to allow my friend to make a project entry 'sticky'.</p>
<p>I had been struggling to make this work:</p>
<pre><code>{# get all sticky Project entries #}
{% set stickyProjects = craft.entries().section('project').limit(30).sticky('1') %}
{% set stickyProjectEntries = stickyProjects.all() %}   

{# get all non sticky Project entries #}
{% set projects = craft.entries().section('project').limit(30).sticky('0') %}
{% set projectEntries = projects.all() %}   
        
{# Fetch, then merge, categories related to both sticky and non-sticky Projects #}
{% set stickyCategories = craft.categories.relatedTo({ sourceElement: stickyProjectEntries }).all() %}
{% set nonStickyCategories = craft.categories.relatedTo({ sourceElement: projectEntries }).all() %}

{% set usedCategories = stickyCategories|merge(nonStickyCategories) %}
</code></pre>
<p>Even after using the Merge filter, &quot;usedCategories&quot; was outputting a duplicate category &quot;<strong>External</strong>&quot; (see image)</p>
<p><a href="https://i.sstatic.net/cDssq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/cDssq.png" alt="enter image description here" /></a></p>

<templating><merge>
Merge list of categories related to 'project' entries - not working, a solution
# Merge list of categories related to 'project' entries - not working, a solution

(Asked by: username_0 on 2020-11-02)

good to be back)
Helping a friend out with a website for his tiling business. The site contains a 'projects' section which displays category-filter buttons allowing the user to toggle relevant projects.
The CMS also contains a custom field to allow my friend to make a project entry 'sticky'.
I had been struggling to make this work:
{# get all sticky Project entries #}
{% set stickyProjects = craft.entries().section('project').limit(30).sticky('1') %}
{% set stickyProjectEntries = stickyProjects.all() %}   

{# get all non sticky Project entries #}
{% set projects = craft.entries().section('project').limit(30).sticky('0') %}
{% set projectEntries = projects.all() %}   
        
{# Fetch, then merge, categories related to both sticky and non-sticky Projects #}
{% set stickyCategories = craft.categories.relatedTo({ sourceElement: stickyProjectEntries }).all() %}
{% set nonStickyCategories = craft.categories.relatedTo({ sourceElement: projectEntries }).all() %}

{% set usedCategories = stickyCategories|merge(nonStickyCategories) %}

Even after using the Merge filter, "usedCategories" was outputting a duplicate category "External" (see image)

**Tags:** templating, merge

---

## 1 answers

### Answer by username_0 on 2020-11-02. Score: 1

Instead of using the Merge filter, I tried "Passing Multiple Relation Criteria" from the Craft Docs, replacing "and" with "or", below...
{# get all sticky Project entries #}
{% set stickyProjects = craft.entries().section('project').limit(30).sticky('1') %}
{% set stickyProjectEntries = stickyProjects.all() %}   

{# get all non sticky Project entries #}
{% set projects = craft.entries().section('project').limit(30).sticky('0') %}
{% set projectEntries = projects.all() %}   
        
{# Fetch, then merge, categories related to both sticky and non-sticky Projects #}
{% set stickyCategories = craft.categories()
    .relatedTo([
    'or',
    { sourceElement: stickyProjectEntries },
    { sourceElement: projectEntries },
    ])
    .all() %}

{% set usedCategories = stickyCategories %}

Which produced the desired result: (image)

Thanks all, I hope that helps you all.
:)


---

