1

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)

enter image description here

1 Answer 1

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) enter image description here

Thanks all, I hope that helps you all.

:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.