Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-27T18:29:16.063Z
Answers.Text: 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.
Answers.UserDisplayName: imnotsure
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-27T18:56:21.857Z
Answers.Text: @imnotsure sorry, it was still not working, I know. Updated again.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-27T22:04:42.857Z
Answers.Text: Im getting error: The merge filter only works with arrays or hashes.
Answers.UserDisplayName: imnotsure
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-28T08:19:45.390Z
Answers.Text: This is still untested, @imnotsure. So please comment back if it works for you now.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-28T15:51:07.437Z
Answers.Text: 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
Answers.UserDisplayName: imnotsure
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-01T10:44:12.103Z
Answers.Text: @imnotsure works now?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-01T23:11:41.867Z
Answers.Text: hi carlcs. amazing, works perfect. Thx alot!
Answers.UserDisplayName: imnotsure
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-01T23:44:46.100Z
Answers.Text: @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.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-01T23:55:50.893Z
Answers.Text: See [this example](http://craftcms.stackexchange.com/a/4992/125) for how you would do this.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-27T08:59:18.077Z
Answers.LastActivityDate: 2014-12-01T10:43:35.800Z
Answers.LastEditDate: 2014-12-01T10:43:35.800Z
Answers.UserDisplayName: carlcs
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-01T23:12:41.437Z
Answers.Text: 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!
Answers.UserDisplayName: imnotsure
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-02T05:39:31.430Z
Answers.Text: 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.
Answers.UserDisplayName: Jérôme Coupé
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-01T22:20:55.940Z
Answers.LastActivityDate: 2014-12-02T05:41:18.103Z
Answers.LastEditDate: 2014-12-02T05:41:18.103Z
Answers.UserDisplayName: Jérôme Coupé
Body: <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>
  
Tags: <templating><relations>
Title: How can I retrieve all the related entries?
ThreadText: # 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`](http://buildwithcraft.com/docs/relations#the-relatedTo-param) 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:
  
  ```
  <h1>{{ entry.title }}</h1>
  {{ 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 %}
  
      <h2>Related Entries</h2>
      <ul>
          {% for relatedEntry in allRelatedEntries %}
              <li>{{ relatedEntry.title }}</li>
          {% endfor %}
      </ul>
  
  {% 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**: @username_0 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, @username_0. 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**: @username_0 works now?  
  > **username_0**: hi username_1. amazing, works perfect. Thx alot!  
  > **username_1**: @username_0 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](http://www.webstoemp.com/blog/manipulating-craft-elementcriteriamodel-with-twig/).
  
  ```
  <h1>{{ entry.title }}</h1>
  {{ 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 #}
  <h2>Related entries</h2>
  {% for entry in relatedItems %}
    <ul>
      <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
    </ul>
  {% else %}
    <p>No related entries found</p>
  {% 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.  
  
  ---
  
  