1

I have a set of entries I am pulling ID. (group) Each of those entries has a Matrix Block. (people) That Matrix Block Type (person) has an entries field. In some cases the same entry (person) is assigned to multiple entries in the group via the Matrix Block. I want to show the matrix blocks from my group only once without duplicates.

Pseudo Code

{% set group = craft.entries.id([100,200,300]) %}

{% for entry in group %}

    {% for person in entry.people %}

        {% if person not already displayed %}

            {% set person_entry = person.entry.one() %}

            {{ person_entry.title }}

        {% endif %}
    
    {% endfor %}

{% endfor %}

1 Answer 1

2

Assuming you can have multiple person blocks per entry (group), you could:

Fetch your entries and eager-load the blocks and their entry to save some overheads

{% set group = craft
    .entries()
    .id([100,200,300])
    .with(['people.person:entry'])
    .all() %}

Then build an array with each person.entry

{# The array to store your person.entry #}
{% set people = [] %}

{# Iterate over your group #}
{% for entry in group %}

    {# Iterate over each blocks and push the person.entry to your people array #}

    {% for person in entry.people %}
        {% set people = people | merge ([person.entry[0]]) %}
    {% endfor %}
{% endfor %}

Then use unique (docs) to dedupe your people as you display:

{% for person in people | unique %}
    {{ person.title }} <br>
{% endfor %}
1
  • Thank you, that worked perfectly. Commented Nov 6, 2020 at 16:05

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.