0

I want to list users that have created entries in a specific channel.

I tried it the Ben Parisak way, but that didn't work out.

here's the code that doesn't work (example 1):

{% set entryIds = craft.entries.section('agenda').datumBeginn('>= ' ~ todayMidnight).limit(null).ids() %}
{% set convivien = craft.users()
    .group('convivien')
    .status('active')
    .cf_convivien_hide('not 1')
    .relatedTo(entryIds)
    .orderBy('firstName')
    .all()
%}

If I had a user field within the agenda channel it would work. But I am trying to get the authors of those entries, and apparently they are not stored as a relation. Maybe this is intentional, maybe not.

Anyways: the workaround I found is really messy (example 2):

{% set entries = craft.entries.section('agenda').datumBeginn('>= ' ~ todayMidnight).limit(null).all() %}
{% set convivien = [] %}
{% for entry in entries %}
    {% set author = entry.author %}
    {% if author.groups[0].name == "Convivien" and not convivien|filter(item => item.id == author.id)|length %}{% set convivien = convivien|merge([{'name': author.name, 'id': author.id}]) %}{% endif %}
{% endfor %}
{% set convivien = convivien|sort %}

if code example 1 would work I could use the same code here:

<select name="convivien" id="veranstalter">
    <option value="">{{ "Kanton wählen"|t }}</option>
    {% for convivium in convivien %}
        <option value="{{ convivium.id }}"{% if convivienParam == convivium.id %} selected="selected"{% endif %}>{{ convivium.name }}</option>
    {% endfor %}
</select>

Does somebody know why example 1 is not working and how I could elegantly achieve this like I was Ben Parisak ;)

2 Answers 2

3

I would try it this way:

{% set authors = craft.entries.section('agenda').datumBeginn('>= ' ~ todayMidnight).select(['author']).all() %}

Authors should be a list of user ids.

Credit to an older post: Get array of titles instead of whole collection of entries' attributes

4
  • Hi, when I use {% set authors = craft.entries.section('agenda').datumBeginn('>= ' ~ todayMidnight).select(['authorId']).asArray().limit(null).all() %}it returns a list of author Id's. But that still leaves me with duplicates and no author names.
    – outline4
    Mar 2 at 9:44
  • You can use array_unique() on the array and then call craft.users.id(authors).orderBy('firstName').all that will give you the list of users or you can do a similar .select(['fullName']) if you just want an array of names
    – cherrykoda
    Mar 2 at 16:09
  • Technically you can pass the array including duplicates to the users query - Craft will return the users without duplicates. But I can understand feeling 'ick' about that.
    – cherrykoda
    Mar 2 at 16:17
  • ok, thanks for the clarification!
    – outline4
    Mar 3 at 11:04
0

after some tinkering chatGPT came up with a rather elegant solution:

{# Today at midnight #}
{% set todayMidnight = now|date('Y-m-d 00:00:00') %}

{# All agenda entries from today #}
{% set entries = craft.entries.section('agenda').datumBeginn('>= ' ~ todayMidnight).limit(null).all() %}

{# Use map and arrow function to get the unique user IDs #}
{% set userIds = entries|map(entry => entry.author.id)|unique %}

{# Get the users based on the user IDs #}
{% set users = craft.users().group('convivien').status('active').cf_convivien_hide('not 1').id(userIds).orderBy('username').all() %}

{# Output the list of users #}
<ul>
  {% for user in users %}
    <li>{{ user.name }}</li>
  {% endfor %}
</ul>

I quite like this solution.

Your Answer

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

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