1

I'm trying to group entries by user on my front-end. I've got so far but I can't seem to get any further to my goal of displaying what type of entries they have posted e.g. lifestyle or tech. to do this I need to get the users entries and query a service dropdown field, if they have lifestyle selected in multiple entries only display 1x tag showing the user has lifestyle posts.

So far I'm just trying to query each user's entries.

{% set listings = craft.entries.section('listings') %}
{% set users = craft.users.group('influencer').all() %}


      {% for user in users %}

          {% if user.getPhoto() %}
              <img class="img-fluid rounded-circle" src="{{ user.getPhoto().getUrl('thumbnail') }}">
          {% else %}
              <img class="img-fluid rounded-circle" src="{{ url('images/user-ph.png') }}">
          {% endif %}

            <h5 class="card-title">{{ user.username }}</h5>
            <small>{{ user.location }}</small>
            <p class="card-text">{{ user.richText }}</p>

        {% for user, entries in craft.entries.find() | group('users') %}
          {% for entry in entries %}
            {{ entry.title }}
          {% endfor %}
        {% endfor %}

      {% endfor %}
2
+50

First off, the easiest way to fetch the user's entries is to do something like this:

{% set entries = craft.entries.authorId(user.id) %}
{% for entry in entries %}
    {{ entry.title }}
{% endfor %}

The way this works is you fetch all the entries that were authored by the current user in your main for loop by using authorId(user.id). In my opinion, this is a lot cleaner and more performant over what you were trying to accomplish with the group filter.


Now, to answer your question; here's how I would solve it.

First, create an empty array:

{% set services = [] %}

Then, inside your user's entries for loop, we'd grab the value of the service field (for each entry) and see if it exists in the services array. If not, we'll merge it in.

{% set dropdown = entry.service.label %}
{% if dropdown not in services %}
     {% set services = services | merge([ dropdown ]) %}
{% endif %}

Then we can use this array to display whatever we want on the frontend:

{% for item in services %}
    {{ item }}
{% endfor %}

So, all together:

{% set users = craft.users.group('influencer').all() %}

{% for user in users %}

    {% if user.getPhoto() %}
        <img class="img-fluid rounded-circle" src="{{ user.getPhoto().getUrl('thumbnail') }}">
    {% else %}
        <img class="img-fluid rounded-circle" src="{{ url('images/user-ph.png') }}">
    {% endif %}

    <h5 class="card-title">{{ user.username }}</h5>
    <small>{{ user.location }}</small>
    <p class="card-text">{{ user.richText }}</p>

    {% set entries = craft.entries.authorId(user.id) %}
    {% set services = [] %}
    {% for entry in entries %}
        {% set dropdown = entry.services.label %}
        {% if dropdown not in services %}
            {% set services = services | merge([ dropdown ]) %}
        {% endif %}
    {% endfor %}

    {% for item in services %}
        {{ item }}
    {% endfor %}

{% endfor %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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