I'm trying to build a filter for a blog section in Craft 3.
I'm fairly new to Craft and my client now wants a dropdown filter where they can filter out authors. Thus, I want to write a query that fetches a unique list of authors from my blog channel structure.
The structure is:
section: blogEntries
(Channel) with an entry type blogEntry
.
Each blogEntry
has a content
(matrix) and and writer
entry field which is connected to one or more employee
entries.
So; blogEntries > blogEntry > writer(s) > employee
What I want is something like
craft.entries.section('blogEntries').column('writer').distinct().all()
This is what I have:
{# Way too slow #}
{% for item in craft.entries.section('blogEntries').all() %}
{% if item.writer|length %}
{% set person = item.writer.one() %}
{% set name = person.firstname ~ (person.surname|length ? ' ' ~ person.surname : '') %}
{% if name not in writers %}
{% set writers = writers|merge([name]) %}
{% endif %}
{% endif %}
{% endfor %}
Terrible solution, terrible performance.
How can I improve this code? Any ideas would be appreciated.