<p>Your code isn't working because you set the related game(s) to a variable <code>entries</code> (i.e. <code>{% set entries = craft.entries...%}</code>), but you're not actually <em>doing</em> anything with that variable. Then you try to render the link to the related game(s), but you're simply re-using the <code>entry</code> variable – which refers to the review entry, not the related game entry.</p>

<p>If you change </p>

<pre><code>{% set entries = craft.entries.section('games').relatedTo(entry) %}
&lt;h3&gt;Game link:&lt;/h3&gt;
...
</code></pre>

<p>to something like</p>

<pre><code>{% set relatedGames = craft.entries.section('games').relatedTo(entry) %}
{% if relatedGames|length %}
    &lt;h3&gt;Game Link:&lt;/h3&gt;
    &lt;ul&gt;
        {% for game in relatedGames %}
            &lt;li&gt;&lt;a href="{{ game.url }}" title="{{ game.title }}"&gt;{{ game.title }}&lt;/a&gt;&lt;/li&gt;
        {% endfor %}
    &lt;/ul&gt;
{% endif %}
</code></pre>

<p>...it should work.</p>

<p>Note that in some cases, its a good idea to be as specific as possible when you're dealing with <a href="https://craftcms.com/docs/relations" rel="nofollow noreferrer">relations</a>. Simply doing <code>relatedTo(entry)</code> will work, but you can also let Craft know where the relation was created (i.e. in which field), and whether the related elements you're looking for is the <em>source</em> or the <em>target</em> elements for the relation. In this case, then Entries field is on the game entry, which means that the review entry is the <em>target</em> element (i.e. the entry that has been selected in an Entries field), and the game entry is the <em>source</em> element (i.e. the entry that actually has the Entries field in its field layout).</p>

<p>Assuming your Entries field on the game entry has a handle <code>relatedReviews</code>, a more specific <code>relatedTo</code> query for your use case could look like this:</p>

<pre><code>{% set relatedGames = craft.entries.section('games').relatedTo({
    field: 'relatedReviews',
    targetElement: entry
}) %}
...
</code></pre>

<p>Being specific about relations can be more performant, and helps avoid issues where you might have multiple relations involving the same elements (e.g. if your review and game entries where also related via a different Entries field).</p>

<p>Another detail to note is that whenever you're dealing with the <em>source</em> element (e.g. if this was the template for the game entry, and you wanted to list the related review entries), you rarely need <code>relatedTo</code> – you can just do this (of course, replace <code>relatedReviews</code> with the real handle for your Entries field):</p>

<pre><code>{% set relatedReviews = entry.relatedReviews %}
{% for review in relatedReviews %}
    &lt;a href="{{ review.url }}"&gt;{{ review.title }}&lt;/a&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-12-29T00:55:40.807Z
Your code worked perfect, thanks! I do have related reviews listed on the game entry templates. I am doing it in the following way but would love to know if my code is less-efficient than yours: {% set reviews = entry.GamesReviews  %}{% if reviews|length %}<h3>Reviews:</h3><ul>{% for review in reviews.order('title asc') %} <li><a href="{{ review.url }}" title="{{ review.title }}">{% for category in review.ReviewsMedia %}{{ category.title }}{% endfor %} - {{ review.title }}</a></li>{% endfor %}</ul>{% endif %}
DevServe
CC BY-SA 3.0
2016-12-28T08:50:32.097Z
2016-12-28T08:50:32.097Z
Mats Mikkel Rummelhoff
<p>I have a section called reviews and a section called games.
A entry field in games is creating a relationship to reviews.</p>

<p>On my _entry.html entry for reviews, I want to link back to the related game.</p>

<p>My code is below. I understand why it isn't working, but not sure how to fix it.</p>

<p>The template displays the Review entry title, the body some extra fields in reviews.</p>

<p>And then gets to the Game Link, but the link is not to my Games section but to my Reviews section.</p>

<p>In other words, on this review:
<a href="http://www.colorcomputergames.com/reviews/1984-01-01/glaxxons" rel="nofollow noreferrer">http://www.colorcomputergames.com/reviews/1984-01-01/glaxxons</a></p>

<p>The link at the bottom should be to:
<a href="http://www.colorcomputergames.com/games/glaxxons" rel="nofollow noreferrer">http://www.colorcomputergames.com/games/glaxxons</a></p>

<p>But instead, goes to:
<a href="http://www.colorcomputergames.com/reviews/1984-01-01/glaxxons" rel="nofollow noreferrer">http://www.colorcomputergames.com/reviews/1984-01-01/glaxxons</a></p>

<pre><code>&lt;h1&gt;{{ entry.title }}&lt;/h1&gt;
&lt;ul&gt;
    {% if entry.ReviewsAuthor|length %}&lt;li&gt;Reviewer: {{ entry.ReviewsAuthor }}&lt;/li&gt;{% endif %}
    {% if entry.ReviewsMedia|length %}&lt;li&gt;Media: {% for category in entry.ReviewsMedia %}&lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;{% endfor %}&lt;/li&gt;{% endif %}
    {% if entry.ReviewsDate|length %}&lt;li&gt;Date: {{ entry.ReviewsDate.format('F Y') }}&lt;/li&gt;{% endif %}
    {% if entry.ReviewsRelatedLink|length %}&lt;li&gt;Related Link: {{ entry.ReviewsRelatedLink }}&lt;/li&gt;{% endif %}
&lt;/ul&gt;
{{ entry.body }}

{% set entries = craft.entries.section('games').relatedTo(entry) %}
&lt;h3&gt;Game Link:&lt;/h3&gt;
    &lt;ul&gt;                      
        &lt;li&gt;&lt;a href="{{ entry.url }}" title="{{ entry.title }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
</code></pre>

<p>As mentioned, in section called Games, I have a field called GamesReview and set it to entries and assign the review entry. Do I need to do the opposite as well t make this work? That is, in my reviews section, create a field called GameReviewed and set it to entries and assign it to Games section.</p>

<p>Thanks</p>

<relations><sections>
Link to an entry to a related section
# Link to an entry to a related section

(Asked by: username_0 on 2016-12-28)

I have a section called reviews and a section called games.
A entry field in games is creating a relationship to reviews.
On my _entry.html entry for reviews, I want to link back to the related game.
My code is below. I understand why it isn't working, but not sure how to fix it.
The template displays the Review entry title, the body some extra fields in reviews.
And then gets to the Game Link, but the link is not to my Games section but to my Reviews section.
In other words, on this review:
http://www.colorcomputergames.com/reviews/1984-01-01/glaxxons
The link at the bottom should be to:
http://www.colorcomputergames.com/games/glaxxons
But instead, goes to:
http://www.colorcomputergames.com/reviews/1984-01-01/glaxxons
{{ entry.title }}

    {% if entry.ReviewsAuthor|length %}Reviewer: {{ entry.ReviewsAuthor }}{% endif %}
    {% if entry.ReviewsMedia|length %}Media: {% for category in entry.ReviewsMedia %}{{ category.title }}{% endfor %}{% endif %}
    {% if entry.ReviewsDate|length %}Date: {{ entry.ReviewsDate.format('F Y') }}{% endif %}
    {% if entry.ReviewsRelatedLink|length %}Related Link: {{ entry.ReviewsRelatedLink }}{% endif %}

{{ entry.body }}

{% set entries = craft.entries.section('games').relatedTo(entry) %}
Game Link:
                          
        {{ entry.title }}
    

As mentioned, in section called Games, I have a field called GamesReview and set it to entries and assign the review entry. Do I need to do the opposite as well t make this work? That is, in my reviews section, create a field called GameReviewed and set it to entries and assign it to Games section.
Thanks

**Tags:** relations, sections

---

## 1 answers

### Answer by username_1 on 2016-12-28. Score: 1 | Accepted answer

Your code isn't working because you set the related game(s) to a variable entries (i.e. {% set entries = craft.entries...%}), but you're not actually doing anything with that variable. Then you try to render the link to the related game(s), but you're simply re-using the entry variable – which refers to the review entry, not the related game entry.
If you change 
{% set entries = craft.entries.section('games').relatedTo(entry) %}
Game link:
...

to something like
{% set relatedGames = craft.entries.section('games').relatedTo(entry) %}
{% if relatedGames|length %}
    Game Link:
    
        {% for game in relatedGames %}
            {{ game.title }}
        {% endfor %}
    
{% endif %}

...it should work.
Note that in some cases, its a good idea to be as specific as possible when you're dealing with relations. Simply doing relatedTo(entry) will work, but you can also let Craft know where the relation was created (i.e. in which field), and whether the related elements you're looking for is the source or the target elements for the relation. In this case, then Entries field is on the game entry, which means that the review entry is the target element (i.e. the entry that has been selected in an Entries field), and the game entry is the source element (i.e. the entry that actually has the Entries field in its field layout).
Assuming your Entries field on the game entry has a handle relatedReviews, a more specific relatedTo query for your use case could look like this:
{% set relatedGames = craft.entries.section('games').relatedTo({
    field: 'relatedReviews',
    targetElement: entry
}) %}
...

Being specific about relations can be more performant, and helps avoid issues where you might have multiple relations involving the same elements (e.g. if your review and game entries where also related via a different Entries field).
Another detail to note is that whenever you're dealing with the source element (e.g. if this was the template for the game entry, and you wanted to list the related review entries), you rarely need relatedTo – you can just do this (of course, replace relatedReviews with the real handle for your Entries field):
{% set relatedReviews = entry.relatedReviews %}
{% for review in relatedReviews %}
    {{ review.title }}
{% endfor %}

> **Comments:**
> **username_0**: Your code worked perfect, thanks! I do have related reviews listed on the game entry templates. I am doing it in the following way but would love to know if my code is less-efficient than yours: {% set reviews = entry.GamesReviews  %}{% if reviews|length %}<h3>Reviews:</h3><ul>{% for review in reviews.order('title asc') %} <li><a href="{{ review.url }}" title="{{ review.title }}">{% for category in review.ReviewsMedia %}{{ category.title }}{% endfor %} - {{ review.title }}</a></li>{% endfor %}</ul>{% endif %}  

---

