2

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?

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 %}
|improve this answer|||||
  • 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 Oct 16 '15 at 13:55
  • Did you try the 2nd approach I posted? I think it could work for your use case. – carlcs Oct 16 '15 at 17:16
  • If I use entry in a relatedTo param, I just get an error saying Variable "entry" does not exist – darylknight Nov 28 '15 at 14:36

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.