<p>Set up a criteria model with both of your <code>relatedTo</code> conditions and add the hierarchy to the list of returned entries using Craft's <code>group</code> filter.</p>

<pre><code>{% set relatedEntries = craft.entries({
    section: 'survivorContent',
    relatedTo: [
        'and',
        {targetElement: entry.themesField},
        {targetElement: entry.survivorsField}
    ]
}) %}

{% set relatedEntriesGrouped = relatedEntries|group('themesField.first().title') %}

{% for title, entries in relatedEntriesGrouped %}
    {{ title }}
    {% for entry in entries %}
        {{ entry.title }}
    {% endfor %}
{% endfor %}
</code></pre>

<p>In case you don't have a "max. 1 element" limit set on your relationship fields, you might also want to look into something like the following.</p>

<pre><code>{% for theme in entry.themesField %}

    {% set relatedEntries = craft.entries({
        section: 'survivorContent',
        relatedTo: [
            'and',
            {targetElement: theme},
            {targetElement: entry.survivorsField}
        ]
    }) %}

    {% if relatedEntries|length %}
        {{ theme.title }}
        {% for entries in relatedEntries %}
            {{ entry.title }}
        {% endfor %}
    {% endif %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-10-16T13:55:20.950Z
Thanks for the quick reply. I think your answer assumes the relationships originate at the survivor when actually, both relationships are from the survivorContent channel. That's what's messing me up. I edited my original question to clarify the relationships
miromo
CC BY-SA 3.0
2015-10-16T17:16:23.573Z
Did you try the 2nd approach I posted? I think it could work for your use case.
carlcs
CC BY-SA 3.0
2015-11-28T14:36:41.790Z
If I use `entry` in a relatedTo param, I just get an error saying `Variable "entry" does not exist`
darylknight
CC BY-SA 3.0
2015-10-16T06:59:24.617Z
2015-10-16T07:13:02.130Z
2015-10-16T07:13:02.130Z
carlcs
<p>I have 2 channels:survivors, survivorContent and a Category:themes. Relationships are as follows:
survivorContent has -> related themes (max 3) AND survivorContent has -> related survivors (max 1)</p>

<p>Themes are always only one level deep.</p>

<p>I have a template that receives survivor.slug as a variable (based on a route). Given the survivor slug, I need to show a list of distinct themes that are <em>indirectly</em> related to this survivor (via the survivorContent->survivor relationship and the survivorContent->themes relationship). Then I need to show the survivorContent entries under each of those filtered themes that are related to both that theme and the given survivor to get output like this:</p>

<ul>
<li>Survivor.title</li>
<li><ul>
<li>Related Theme 1</li>
</ul></li>
<li><ul>
<li><ul>
<li>Related survivorContent1 (related to both the Survivor &amp; Theme 1)</li>
</ul></li>
</ul></li>
<li><ul>
<li><ul>
<li>Related survivorContent2 (related to both the Survivor &amp; Theme 1)</li>
</ul></li>
</ul></li>
<li><ul>
<li>Related Theme 2</li>
</ul></li>
<li><ul>
<li><ul>
<li>Related survivorContent3 (related to both the Survivor &amp; Theme 2)</li>
</ul></li>
</ul></li>
<li><ul>
<li><ul>
<li>Related survivorContent1 (related to both the Survivor &amp; Theme 2 as well as Theme 1, above)</li>
</ul></li>
</ul></li>
<li><ul>
<li>etc...</li>
</ul></li>
</ul>

<p>I'm struggling to find a way to do this that is remotely efficient. Any ideas?</p>

<relations><templating>
How do I display entries, grouped by distinct categories where the entries share a relationship to both the category and an entry in another channel
# How do I display entries, grouped by distinct categories where the entries share a relationship to both the category and an entry in another channel

(Asked by: username_0 on 2015-10-16)

I have 2 channels:survivors, survivorContent and a Category:themes. Relationships are as follows:
survivorContent has -> related themes (max 3) AND survivorContent has -> related survivors (max 1)

Themes are always only one level deep.

I have a template that receives survivor.slug as a variable (based on a route). Given the survivor slug, I need to show a list of distinct themes that are *indirectly* related to this survivor (via the survivorContent->survivor relationship and the survivorContent->themes relationship). Then I need to show the survivorContent entries under each of those filtered themes that are related to both that theme and the given survivor to get output like this:

* Survivor.title
* + Related Theme 1
* + - Related survivorContent1 (related to both the Survivor & Theme 1)
* + - Related survivorContent2 (related to both the Survivor & Theme 1)
* + Related Theme 2
* + - Related survivorContent3 (related to both the Survivor & Theme 2)
* + - Related survivorContent1 (related to both the Survivor & Theme 2 as well as Theme 1, above)
* + etc...

I'm struggling to find a way to do this that is remotely efficient. Any ideas?

**Tags:** relations, templating


---

## 1 answers

### Answer by username_1 on 2015-10-16. Score: 2

Set up a criteria model with both of your relatedTo conditions and add the hierarchy to the list of returned entries using Craft's group filter.
{% set relatedEntries = craft.entries({
    section: 'survivorContent',
    relatedTo: [
        'and',
        {targetElement: entry.themesField},
        {targetElement: entry.survivorsField}
    ]
}) %}

{% set relatedEntriesGrouped = relatedEntries|group('themesField.first().title') %}

{% for title, entries in relatedEntriesGrouped %}
    {{ title }}
    {% for entry in entries %}
        {{ entry.title }}
    {% endfor %}
{% endfor %}

In case you don't have a "max. 1 element" limit set on your relationship fields, you might also want to look into something like the following.
{% for theme in entry.themesField %}

    {% set relatedEntries = craft.entries({
        section: 'survivorContent',
        relatedTo: [
            'and',
            {targetElement: theme},
            {targetElement: entry.survivorsField}
        ]
    }) %}

    {% if relatedEntries|length %}
        {{ theme.title }}
        {% for entries in relatedEntries %}
            {{ entry.title }}
        {% endfor %}
    {% endif %}

{% endfor %}

> **Comments:**
> **username_0**: Thanks for the quick reply. I think your answer assumes the relationships originate at the survivor when actually, both relationships are from the survivorContent channel. That's what's messing me up. I edited my original question to clarify the relationships  
> **username_1**: Did you try the 2nd approach I posted? I think it could work for your use case.  
> **username_2**: If I use `entry` in a relatedTo param, I just get an error saying `Variable "entry" does not exist`  

---

