<p>I spot a couple of Twig errors in your code.</p>

<p>First, in your <code>for</code> loop you're casting the entries returned by <code>craft.entries</code> to a variable <code>news</code>, but you're calling <code>entry.getAuthor()</code> inside the loop. Either change <code>news</code> to <code>entry</code> or <code>entry</code> to <code>news</code>.</p>

<p>Second, I'm not sure if <code>craft.entries('news')</code> actually returns anything (or at least not what you actually want) – it should probably be <code>craft.entries.section('news')</code>.</p>

<p>Putting it together, this should work:</p>

<pre><code>{% for entry in craft.entries.section('news') %}
    ...
    {{ dump(entry.getAuthor().getGroups()) }}
{% endfor %}
</code></pre>

<p>Also, since you're not reporting any error messages output by Craft, I'm guessing you haven't enabled <a href="https://craftcms.com/docs/config-settings#devMode" rel="nofollow"><code>devMode</code></a> in your development environment. It's a really good idea to have <code>devMode</code> set to <code>true</code> while you're working on your code, because then Craft will report any syntax errors (like your undeclared <code>entry</code> variable) which will make it way easier for you to debug.</p>

<p>Just remember to never have <code>devMode</code> enabled for production environments – it's both a performance and security issue if you do.</p>

CC BY-SA 3.0
2016-02-04T14:41:18.127Z
Sorry I mistyped the example (the code is a bit more complex than that). So yes craft.entries.section('news') is the correct code I use for that loop, that why I don't have any error...
pvledoux
CC BY-SA 3.0
2016-02-04T15:04:54.663Z
Ok, make sure you have `{% for entry...` and not `{% for news...` as well, and your code should be legit. In which case, there are only two reasons I can think of that could cause `entry.getAuthor().getGroups()` to not return anything: 1. Singles doesn't have authors, so if the `news` section is a Single, that'd be the issue. 2. Obviously, `getGroups()` will return an empty array for users who aren't actually in any groups. Are you absolute sure your entry author(s) are assigned to 1 or more groups?
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-02-04T15:13:12.820Z
Sorry, I just realized that the `news` vs. `entry` issue isn't a typo at all like I thought, but that the `entry` variable is really unrelated to the loop and declared outside it. Is `entry` a Single? If not, can you confirm that its author is a member of one or more user groups?
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-02-04T15:15:43.747Z
Ops sorry I have edited it too fast. Actuality, I think I have found the issue. The list is paginated, and it looks like paginated entries are not returning the entire Entry Model for some reason.
pvledoux
CC BY-SA 3.0
2016-02-04T14:29:55.837Z
2016-02-04T14:29:55.837Z
Mats Mikkel Rummelhoff
<p>I'm trying to output users groups for every entries in a for loop, but it returns an empty array:</p>

<pre><code>{%for entry in craft.entries.section('news') %}
    ...
    {{ dump(entry.getAuthor().getGroups()) }}
{% endfor %}
</code></pre>

<p>But it's working correctly in the entry page itself.</p>

<p>Any idea how I can return the groups in a for loop?</p>

<user-groups><usermodel>
entry.getAuthor().getGroups() always return empty array
# entry.getAuthor().getGroups() always return empty array

(Asked by: username_0 on 2016-02-04)

I'm trying to output users groups for every entries in a for loop, but it returns an empty array:
{%for entry in craft.entries.section('news') %}
    ...
    {{ dump(entry.getAuthor().getGroups()) }}
{% endfor %}

But it's working correctly in the entry page itself.
Any idea how I can return the groups in a for loop?

**Tags:** user-groups, usermodel

---

## 1 answers

### Answer by username_1 on 2016-02-04. Score: 1

I spot a couple of Twig errors in your code.
First, in your for loop you're casting the entries returned by craft.entries to a variable news, but you're calling entry.getAuthor() inside the loop. Either change news to entry or entry to news.
Second, I'm not sure if craft.entries('news') actually returns anything (or at least not what you actually want) – it should probably be craft.entries.section('news').
Putting it together, this should work:
{% for entry in craft.entries.section('news') %}
    ...
    {{ dump(entry.getAuthor().getGroups()) }}
{% endfor %}

Also, since you're not reporting any error messages output by Craft, I'm guessing you haven't enabled devMode in your development environment. It's a really good idea to have devMode set to true while you're working on your code, because then Craft will report any syntax errors (like your undeclared entry variable) which will make it way easier for you to debug.
Just remember to never have devMode enabled for production environments – it's both a performance and security issue if you do.

> **Comments:**
> **username_0**: Sorry I mistyped the example (the code is a bit more complex than that). So yes craft.entries.section('news') is the correct code I use for that loop, that why I don't have any error...  
> **username_1**: Ok, make sure you have `{% for entry...` and not `{% for news...` as well, and your code should be legit. In which case, there are only two reasons I can think of that could cause `entry.getAuthor().getGroups()` to not return anything: 1. Singles doesn't have authors, so if the `news` section is a Single, that'd be the issue. 2. Obviously, `getGroups()` will return an empty array for users who aren't actually in any groups. Are you absolute sure your entry author(s) are assigned to 1 or more groups?  
> **username_1**: Sorry, I just realized that the `news` vs. `entry` issue isn't a typo at all like I thought, but that the `entry` variable is really unrelated to the loop and declared outside it. Is `entry` a Single? If not, can you confirm that its author is a member of one or more user groups?  
> **username_0**: Ops sorry I have edited it too fast. Actuality, I think I have found the issue. The list is paginated, and it looks like paginated entries are not returning the entire Entry Model for some reason.  

---

