<p>With help from Robin (getting the ids), <a href="https://straightupcraft.com/articles/introduction-to-relations-and-reverse-relations-in-craft-cms-with-examples" rel="nofollow noreferrer">this article</a> from Straight Up Craft, and a bit of trial and error, I ended up solving my issue with this:</p>

<pre><code>{% set people = craft.users.group('family').ids() %}
{% set relatedPlaces = craft.entries.section('locations').relatedTo({
    targetElement: people,
    field: 'relatedPeople'
}) %}

{% set markers = '' %}
{% set markerInfo = '' %}   

{%- for place in relatedPlaces.all() -%}
    {% set relatedPeople = craft.users.group('family').relatedTo(place) %}
    {%- set markers -%}
        {% if not loop.first %}{{ markers }}{% endif %} ['{{ place.location.address }}', {{ place.location.lat }},{{ place.location.lng }}],
    {%- endset -%}
    {%- set markerInfo -%}
        {% if not loop.first %}{{ markerInfo }}{% endif %} ['&lt;div class="info_content"&gt;' + '&lt;h5&gt;{{ place.title|e('js') }}&lt;/h5&gt;' + '{%- for person in relatedPeople -%}{{ person.firstName }} {{ person.lastName }}&lt;br&gt;{%- endfor -%}' + '&lt;/div&gt;'],
    {%- endset -%}
{%- endfor -%}
</code></pre>

CC BY-SA 4.0
2018-06-25T10:43:29.393Z
2018-06-25T10:43:29.393Z
Adam George
<pre><code>{% set people = craft.users.group('family').ids() %}
{% set relatedPlaces = craft.entries.section('locations').relatedTo({
    targetElement: person,
    field: 'relatedPeople'
}) %}
</code></pre>

<p>Grab all the ids from your users, then find all locations related to those. This way you don't need multiple loops and get each location once and not multiple times </p>

CC BY-SA 4.0
2018-06-25T10:38:33.757Z
This didn't work, but using the ids got me on the right track - thank you!
Adam George
CC BY-SA 4.0
2018-06-23T08:22:09.980Z
2018-06-23T08:22:09.980Z
Robin Schambach
<p>I have a relationship between users and 'locations', and I'm using the code below to output some markers for a map.</p>

<p>This all works fine except for two things:</p>

<ol>
<li>I'm repeating a couple of 'for' loops. When I try not to repeat
them, the 'markers' and 'markerInfo' return 'undefined'.</li>
<li>Within markerInfo, I'd like to list the names (e.g. 'user.firstName') of the users related to that location, but when I try, I get a list of ALL users that have a relationship with ANY location.</li>
</ol>

<p>In plain language, what I'm trying to do is:</p>

<ul>
<li>Find me all the users who are part of the 'family' group.</li>
<li>Find me all the locations which are related to those users and plot each
one (but only once for each location).</li>
<li>For each location, give me a list of the users that have a relationship with it.</li>
</ul>

<p>--</p>

<ul>
<li>My <em>source elements</em> are entries in the section 'locations', with the
field 'relatedPeople'. </li>
<li>My <em>target elements</em> are 'users' (from the group
'family').</li>
</ul>

<p><strong>How can I make this more performant (not repeat things), and get a list of users per location within 'markerInfo'?</strong></p>

<pre><code>{% set people = craft.users.group('family') %}

{% set markers %}
    {%- for person in people.all() -%}
        {% set relatedPlaces = craft.entries.section('locations').relatedTo({
            targetElement: person,
            field: 'relatedPeople'
        }) %}
        {%- for place in relatedPlaces.all() -%}
        ['{{ place.location.address }}', {{ place.location.lat }},{{ place.location.lng }}],
        {%- endfor -%}
    {%- endfor -%}
{% endset %}

{% set markerInfo %}    
    {%- for person in people.all() -%}
        {% set relatedPlaces = craft.entries.section('locations').relatedTo({
            targetElement: person,
            field: 'relatedPeople'
        }) %}
        {%- for place in relatedPlaces.all() -%}
        ['&lt;div class="info_content"&gt;' + '&lt;h5&gt;{{ place.title|e('js') }}&lt;/h5&gt;' + '{% if place.startDate|length %}{{ place.startDate | date('M Y') }}{% endif %}{% if place.startDate|length and place.finishDate|length%} to {% endif %}{% if place.finishDate|length %}{{ place.finishDate | date('M Y') }}{% endif %}' + '&lt;/div&gt;'],
        {%- endfor -%}
    {%- endfor -%}
{% endset %}
</code></pre>

<users><relatedto>
Reverse relationships with entries and users
# Reverse relationships with entries and users

(Asked by: username_0 on 2018-06-17)

I have a relationship between users and 'locations', and I'm using the code below to output some markers for a map.
This all works fine except for two things:

I'm repeating a couple of 'for' loops. When I try not to repeat
them, the 'markers' and 'markerInfo' return 'undefined'.
Within markerInfo, I'd like to list the names (e.g. 'user.firstName') of the users related to that location, but when I try, I get a list of ALL users that have a relationship with ANY location.

In plain language, what I'm trying to do is:

Find me all the users who are part of the 'family' group.
Find me all the locations which are related to those users and plot each
one (but only once for each location).
For each location, give me a list of the users that have a relationship with it.

--

My source elements are entries in the section 'locations', with the
field 'relatedPeople'. 
My target elements are 'users' (from the group
'family').

How can I make this more performant (not repeat things), and get a list of users per location within 'markerInfo'?
{% set people = craft.users.group('family') %}

{% set markers %}
    {%- for person in people.all() -%}
        {% set relatedPlaces = craft.entries.section('locations').relatedTo({
            targetElement: person,
            field: 'relatedPeople'
        }) %}
        {%- for place in relatedPlaces.all() -%}
        ['{{ place.location.address }}', {{ place.location.lat }},{{ place.location.lng }}],
        {%- endfor -%}
    {%- endfor -%}
{% endset %}

{% set markerInfo %}    
    {%- for person in people.all() -%}
        {% set relatedPlaces = craft.entries.section('locations').relatedTo({
            targetElement: person,
            field: 'relatedPeople'
        }) %}
        {%- for place in relatedPlaces.all() -%}
        ['' + '{{ place.title|e('js') }}' + '{% if place.startDate|length %}{{ place.startDate | date('M Y') }}{% endif %}{% if place.startDate|length and place.finishDate|length%} to {% endif %}{% if place.finishDate|length %}{{ place.finishDate | date('M Y') }}{% endif %}' + ''],
        {%- endfor -%}
    {%- endfor -%}
{% endset %}

**Tags:** users, relatedto

---

## 2 answers

### Answer by username_0 on 2018-06-25. Score: 2 | Accepted answer

With help from Robin (getting the ids), this article from Straight Up Craft, and a bit of trial and error, I ended up solving my issue with this:
{% set people = craft.users.group('family').ids() %}
{% set relatedPlaces = craft.entries.section('locations').relatedTo({
    targetElement: people,
    field: 'relatedPeople'
}) %}

{% set markers = '' %}
{% set markerInfo = '' %}   

{%- for place in relatedPlaces.all() -%}
    {% set relatedPeople = craft.users.group('family').relatedTo(place) %}
    {%- set markers -%}
        {% if not loop.first %}{{ markers }}{% endif %} ['{{ place.location.address }}', {{ place.location.lat }},{{ place.location.lng }}],
    {%- endset -%}
    {%- set markerInfo -%}
        {% if not loop.first %}{{ markerInfo }}{% endif %} ['' + '{{ place.title|e('js') }}' + '{%- for person in relatedPeople -%}{{ person.firstName }} {{ person.lastName }}{%- endfor -%}' + ''],
    {%- endset -%}
{%- endfor -%}


---

### Answer by username_1 on 2018-06-23. Score: 3

{% set people = craft.users.group('family').ids() %}
{% set relatedPlaces = craft.entries.section('locations').relatedTo({
    targetElement: person,
    field: 'relatedPeople'
}) %}

Grab all the ids from your users, then find all locations related to those. This way you don't need multiple loops and get each location once and not multiple times

> **Comments:**
> **username_0**: This didn't work, but using the ids got me on the right track - thank you!  

---

