4

I'm working on a site that has two different entry types (e.g., "blog" and "photos"), and each type has it's own tag group. When viewing a particular entry, I'd like to display related entries found in both entry types.

So, for example, let's say that I've written "Entry A" and "Entry B" -- "Entry A" is in "blog" and "Entry B" is a "photos." They both have the same tag ("foo"), but that tag exists in each entry type's respective tag group.

If I view "Entry A," can I display a list of related entries that includes "Entry B" even though the tag that they share isn't in the same tag group?

  • So, it's not the same tag? It's two different tags, they just happen to have the same name? – André Elvan Sep 13 '14 at 21:30
  • That is correct. Technically, they're two different tags in two different tag groups assigned to two different entry types. – Jason Morehead Sep 15 '14 at 1:38
4

Ok, I believe this does what you want:

{% if entry.tags1 | length %}

    {# get tag names from our current entry #}
    {% set entryTagNames = [] %}
    {% for tag in entry.tags1 %}
        {% set entryTagNames = entryTagNames | merge([tag.name]) %}
    {% endfor %}

    {% if entryTagNames | length %}

        {# get tags from the other tag group with the same names #}
        {% set otherTags = craft.tags({ group: 'group2', name: entryTagNames }) %}

        {# get entries related to these tags #}
        {% set retaledEntries = craft.entries({ section: 'news', relatedTo: otherTags }) %}

        {# output related entries. just debugging that this actually works #}
        {% for relatedEntry in retaledEntries %}
            {{ relatedEntry }}<br>
            {% for relatedEntryTag in relatedEntry.tags2 %}
                - {{ relatedEntryTag.name }}<br>
            {% endfor %}
            <br>
        {% endfor %}

    {% endif %}

{% endif %}

My first section, which the current entry is in, has a field named tags1, connected to the tag group group1. My second section is named news, has a field named tags2, connected to the tag group group2.

It seems a bit sketchy to rely on tag names, but.

| improve this answer | |

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.