Merge list of categories related to 'project' entries - not working, a solution 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, &quot;usedCategories&quot; was outputting a duplicate category &quot;External&quot; (see image)
Instead of using the Merge filter, I tried &quot;Passing Multiple Relation Criteria&quot; from the Craft Docs, replacing &quot;and&quot; with &quot;or&quot;, 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.
:)