<p>This is more tricky then I first thought. I hope this is what you need:</p>

<p>In case the current <code>entry</code> element is not the source of your relation, find that source entry first. This entry then goes into the <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param" rel="nofollow"><code>relatedTo</code></a> parameter of a new ElementCriteriaModel, where we exclude the current <code>entry</code> entry using its ID.</p>

<p>Code is probably more easy to understand than my wobbling english explanation: </p>

<pre><code>&lt;h1&gt;{{ entry.title }}&lt;/h1&gt;
{{ entry.body }}

{# Get `allRelatedEntries` if this `entry` is the "source element" of the relation #}
{% set allRelatedEntries = craft.entries.relatedTo({ sourceElement: entry }) %}

{# Get `allRelatedEntries` if this `entry` is the "target element" of the relation #}
{% if not allRelatedEntries|length %}
    {% set sourceElement = craft.entries.relatedTo({ targetElement: entry }).first() %}

    {# Any relations at all? #}
    {% if sourceElement %}
        {% set targetElements = craft.entries.relatedTo({ sourceElement: sourceElement }).id('not ' ~ entry.id).find() %}
        {% if targetElements|length %}
            {% set allRelatedEntries = targetElements|merge([sourceElement]) %}
        {% endif %}
    {% endif %}

{% endif %}

{% if allRelatedEntries|length %}

    &lt;h2&gt;Related Entries&lt;/h2&gt;
    &lt;ul&gt;
        {% for relatedEntry in allRelatedEntries %}
            &lt;li&gt;{{ relatedEntry.title }}&lt;/li&gt;
        {% endfor %}
    &lt;/ul&gt;

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

CC BY-SA 3.0
2014-11-27T18:29:16.063Z
thanks for your respond but ive tried that. entry 1 shows entry 2 + 3 (as I want). But when im at entry 2 or 3 page, it only shows entry 1.
imnotsure
CC BY-SA 3.0
2014-11-27T18:56:21.857Z
@imnotsure sorry, it was still not working, I know. Updated again.
carlcs
CC BY-SA 3.0
2014-11-27T22:04:42.857Z
Im getting error: The merge filter only works with arrays or hashes.
imnotsure
CC BY-SA 3.0
2014-11-28T08:19:45.390Z
This is still untested, @imnotsure. So please comment back if it works for you now.
carlcs
CC BY-SA 3.0
2014-11-28T15:51:07.437Z
thanks for your help so far. This seems to work perfectly for entries which are related to each other. But my template breaks on those entries which arent related to anything. I get a error: Undefined offset: 0
imnotsure
CC BY-SA 3.0
2014-12-01T10:44:12.103Z
@imnotsure works now?
carlcs
CC BY-SA 3.0
2014-12-01T23:11:41.867Z
hi carlcs. amazing, works perfect. Thx alot!
imnotsure
CC BY-SA 3.0
2014-12-01T23:44:46.100Z
@imnotsure nice! If you need your `relatedEntries` to be sorted differently, you would "collect" the entries' IDs instead of the EntryModels, and then use the IDs with the `id()` parameter in another ElementCriteriaModel where you can set the desired sort order.
carlcs
CC BY-SA 3.0
2014-12-01T23:55:50.893Z
See [this example](http://craftcms.stackexchange.com/a/4992/125) for how you would do this.
carlcs
CC BY-SA 3.0
2014-11-27T08:59:18.077Z
2014-12-01T10:43:35.800Z
2014-12-01T10:43:35.800Z
carlcs
<p>Here is a bare bones example of how I would deal with this.</p>

<p>I would essentially build an array of the ids I need and then retrieve the entries based on those IDs. It's an <a href="http://www.webstoemp.com/blog/manipulating-craft-elementcriteriamodel-with-twig/" rel="nofollow">example of a technique I recently wrote about</a>.</p>

<pre><code>&lt;h1&gt;{{ entry.title }}&lt;/h1&gt;
{{ entry.body }}

{# create empty array #}
{% set relatedItemsIds = [] %}

{# if entry field is not empty, merge entry ids into array #}
{% if entry.newsRelatedItems|length %}
  {% set relatedItemsIds = relatedItemsIds|merge(entry.newsRelatedItems.ids()) %}
{% endif %}

{# find source item in the relationship using the current entry #}
{% set sourceItem = craft.entries.section('news').relatedTo({
  targetElement: entry
}).first() %}

{# 
 # if source item is found
 # - merge its id to the array
 # - merge the ids of its target entries to the array but remove the current entry id from that merged array
 #}
{% if sourceItem %}
  {% set relatedItemsIds = relatedItemsIds|merge(craft.entries.section('news').id(sourceItem.id).ids()) %}
  {% set relatedItemsIds = relatedItemsIds|merge(sourceItem.newsRelatedItems.ids())|without(entry.id) %}
{% endif %}

{# use created array to retrieve an ElementCriteriaModel for the entries you need #}
{% set relatedItems = craft.entries.section('news').id(relatedItemsIds).fixedOrder(true).find() %}

{# display entries #}
&lt;h2&gt;Related entries&lt;/h2&gt;
{% for entry in relatedItems %}
  &lt;ul&gt;
    &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
{% else %}
  &lt;p&gt;No related entries found&lt;/p&gt;
{% endfor %}
</code></pre>

<p>Tested this code on my local sandbox. Can you check if it works for your use case ?</p>

CC BY-SA 3.0
2014-12-01T23:12:41.437Z
Hi Jerome, Ive also tested ur code and it works like a charm! Im going to see which one is performing better. Thanks for ur respond!
imnotsure
CC BY-SA 3.0
2014-12-02T05:39:31.430Z
Glad it helps. Should pretty ok performance-wise. Fetching ids is usually quite lean in terms of queries: in my case I get 17 queries and 15Kb of memory.
Jérôme Coupé
CC BY-SA 3.0
2014-12-01T22:20:55.940Z
2014-12-02T05:41:18.103Z
2014-12-02T05:41:18.103Z
Jérôme Coupé
<p>I have 3 entries. One of the entries got the entries field where Ive attached the two other entries. Im trying to display all the related entries whether entry 1, 2 or 3 is chosen.</p>

<p>For example:</p>

<ul>
<li>Entry 1: Has the entries field. When this entry is selected, I need to show entry 2 and 3</li>
<li>Entry 2:  When this entry is selected I need to show entry 1 and 3 </li>
<li>Entry 3:  When this entry is selected I need to show entry 1 and 2</li>
</ul>

<p>Is this possible?</p>

<templating><relations>
How can I retrieve all the related entries?
# How can I retrieve all the related entries?

(Asked by: username_0 on 2014-11-26)

I have 3 entries. One of the entries got the entries field where Ive attached the two other entries. Im trying to display all the related entries whether entry 1, 2 or 3 is chosen.

For example:

* Entry 1: Has the entries field. When this entry is selected, I need to show entry 2 and 3
* Entry 2: When this entry is selected I need to show entry 1 and 3
* Entry 3: When this entry is selected I need to show entry 1 and 2

Is this possible?

**Tags:** templating, relations

---

## 2 answers

### Answer by username_1 on 2014-11-27. Score: 3 | Accepted answer

This is more tricky then I first thought. I hope this is what you need:
In case the current entry element is not the source of your relation, find that source entry first. This entry then goes into the relatedTo parameter of a new ElementCriteriaModel, where we exclude the current entry entry using its ID.
Code is probably more easy to understand than my wobbling english explanation: 
{{ entry.title }}
{{ entry.body }}

{# Get `allRelatedEntries` if this `entry` is the "source element" of the relation #}
{% set allRelatedEntries = craft.entries.relatedTo({ sourceElement: entry }) %}

{# Get `allRelatedEntries` if this `entry` is the "target element" of the relation #}
{% if not allRelatedEntries|length %}
    {% set sourceElement = craft.entries.relatedTo({ targetElement: entry }).first() %}

    {# Any relations at all? #}
    {% if sourceElement %}
        {% set targetElements = craft.entries.relatedTo({ sourceElement: sourceElement }).id('not ' ~ entry.id).find() %}
        {% if targetElements|length %}
            {% set allRelatedEntries = targetElements|merge([sourceElement]) %}
        {% endif %}
    {% endif %}

{% endif %}

{% if allRelatedEntries|length %}

    Related Entries
    
        {% for relatedEntry in allRelatedEntries %}
            {{ relatedEntry.title }}
        {% endfor %}
    

{% endif %}

> **Comments:**
> **username_0**: thanks for your respond but ive tried that. entry 1 shows entry 2 + 3 (as I want). But when im at entry 2 or 3 page, it only shows entry 1.  
> **username_1**: @imnotsure sorry, it was still not working, I know. Updated again.  
> **username_0**: Im getting error: The merge filter only works with arrays or hashes.  
> **username_1**: This is still untested, @imnotsure. So please comment back if it works for you now.  
> **username_0**: thanks for your help so far. This seems to work perfectly for entries which are related to each other. But my template breaks on those entries which arent related to anything. I get a error: Undefined offset: 0  
> **username_1**: @imnotsure works now?  
> **username_0**: hi carlcs. amazing, works perfect. Thx alot!  
> **username_1**: @imnotsure nice! If you need your `relatedEntries` to be sorted differently, you would "collect" the entries' IDs instead of the EntryModels, and then use the IDs with the `id()` parameter in another ElementCriteriaModel where you can set the desired sort order.  
> **username_1**: See [this example](http://craftcms.stackexchange.com/a/4992/125) for how you would do this.  

---

### Answer by username_2 on 2014-12-01. Score: 1

Here is a bare bones example of how I would deal with this.
I would essentially build an array of the ids I need and then retrieve the entries based on those IDs. It's an example of a technique I recently wrote about.
{{ entry.title }}
{{ entry.body }}

{# create empty array #}
{% set relatedItemsIds = [] %}

{# if entry field is not empty, merge entry ids into array #}
{% if entry.newsRelatedItems|length %}
  {% set relatedItemsIds = relatedItemsIds|merge(entry.newsRelatedItems.ids()) %}
{% endif %}

{# find source item in the relationship using the current entry #}
{% set sourceItem = craft.entries.section('news').relatedTo({
  targetElement: entry
}).first() %}

{# 
 # if source item is found
 # - merge its id to the array
 # - merge the ids of its target entries to the array but remove the current entry id from that merged array
 #}
{% if sourceItem %}
  {% set relatedItemsIds = relatedItemsIds|merge(craft.entries.section('news').id(sourceItem.id).ids()) %}
  {% set relatedItemsIds = relatedItemsIds|merge(sourceItem.newsRelatedItems.ids())|without(entry.id) %}
{% endif %}

{# use created array to retrieve an ElementCriteriaModel for the entries you need #}
{% set relatedItems = craft.entries.section('news').id(relatedItemsIds).fixedOrder(true).find() %}

{# display entries #}
Related entries
{% for entry in relatedItems %}
  
    {{ entry.title }}
  
{% else %}
  No related entries found
{% endfor %}

Tested this code on my local sandbox. Can you check if it works for your use case ?

> **Comments:**
> **username_0**: Hi Jerome, Ive also tested ur code and it works like a charm! Im going to see which one is performing better. Thanks for ur respond!  
> **username_2**: Glad it helps. Should pretty ok performance-wise. Fetching ids is usually quite lean in terms of queries: in my case I get 17 queries and 15Kb of memory.  

---

