Answers.Body: <p>If I understand your structure correctly, something like this should work. First grab the ids, merge them (if they don't already exist in the id array), then query for entries based on ids with sort order defined.</p>
  
  <pre><code>{% set programs1 = craft.entries.section('programs1') %}
  {% set programIds = craft.entries.section('news').relatedTo(programs1).ids() %}
  {% set programs2 = craft.entries.section('programs2') %}
  {% set program2Ids = craft.entries.section('news').relatedTo(programs2).ids() %}
  
  {% for id in program2Ids %}
      {% if id not in programIds %}
          {% set programIds = programIds|merge(id) %}
      {% endif %}
  {% endfor %}
  
  {% set programEntries = craft.entries.section('news').id(programIds).order(dateUpdated).limit(4) %}
  
  {% for entry in programEntries %}
      {{ entry.title }}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-01T18:44:22.753Z
Answers.Text: This looks like a great way to go. I'll let you know how it goes when I implement later today.
Answers.UserDisplayName: Paulo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-01T22:19:37.677Z
Answers.Text: This worked almost perfectly. I had to remove the "order(dateUpdated).limit(4)" from the two relatedTo queries due to a 503 error: "CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'dateUpdated' in order clause is ambiguous"
Answers.UserDisplayName: Paulo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-01T23:38:24.363Z
Answers.Text: Glad it worked. I was trying to save some processing power by limiting the initial results, but it's possible that 'order' and 'limit' is not compatible with `relatedTo()` and/or `ids()` (or maybe the order that they are chained needs to switch—not sure). Regardless, I'll remove it from the answer.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-01T18:12:33.700Z
Answers.LastActivityDate: 2015-05-01T23:38:55.157Z
Answers.LastEditDate: 2015-05-01T23:38:55.157Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>I think I have been staring at this one too long because conceptually it seems fairly straightforward but I can't seem to pull it off. I am pretty sure I am going nowhere due to how complicated I am making my twig code</p>
  
  <p><strong>Here is what I have:</strong></p>
  
  <ul>
  <li>"News" channel that holds news entries that can be optionally related to either a Program 1 entry or Program 2 entry.</li>
  <li>Program Type 1 channel.</li>
  <li>Program Type 2 channel.</li>
  <li>In the news entry there are two fields, one to relate Program 1 entries to the news item and another to relate Program 2 entries to the news item. They are being used similarly to categories in this case.</li>
  <li>News entries can have one or both of the related entries fields populated  </li>
  <li>This is in a module that is shared throughout the site so I am not on a "news entry" page (if that makes sense), it will be an include that is included where needed.</li>
  </ul>
  
  <p><strong>Here is what I need to do</strong></p>
  
  <p>I need to be able to display a list of:</p>
  
  <ul>
  <li>the four most recently updated Program 1 or Program 2 NEWS items (combined list order by dateUpdated)</li>
  </ul>
  
  <p>Thinking out loud it looks like I can grab a listing of each and then merge them together but I would love to see how smarter folks would handle this in the most efficient way.</p>
  
Tags: <relations>
Title: Display entries filtered by one or two related entry fields ordered by "dateUpdated"
ThreadText: # Display entries filtered by one or two related entry fields ordered by "dateUpdated"
  
  (Asked by: username_0 on 2015-05-01)
  
  I think I have been staring at this one too long because conceptually it seems fairly straightforward but I can't seem to pull it off. I am pretty sure I am going nowhere due to how complicated I am making my twig code
  
  **Here is what I have:**
  
  * "News" channel that holds news entries that can be optionally related to either a Program 1 entry or Program 2 entry.
  * Program Type 1 channel.
  * Program Type 2 channel.
  * In the news entry there are two fields, one to relate Program 1 entries to the news item and another to relate Program 2 entries to the news item. They are being used similarly to categories in this case.
  * News entries can have one or both of the related entries fields populated
  * This is in a module that is shared throughout the site so I am not on a "news entry" page (if that makes sense), it will be an include that is included where needed.
  
  **Here is what I need to do**
  
  I need to be able to display a list of:
  
  * the four most recently updated Program 1 or Program 2 NEWS items (combined list order by dateUpdated)
  
  Thinking out loud it looks like I can grab a listing of each and then merge them together but I would love to see how smarter folks would handle this in the most efficient way.
  
  **Tags:** relations
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-05-01. Score: 3 | Accepted answer
  
  If I understand your structure correctly, something like this should work. First grab the ids, merge them (if they don't already exist in the id array), then query for entries based on ids with sort order defined.
  
  ```
  {% set programs1 = craft.entries.section('programs1') %}
  {% set programIds = craft.entries.section('news').relatedTo(programs1).ids() %}
  {% set programs2 = craft.entries.section('programs2') %}
  {% set program2Ids = craft.entries.section('news').relatedTo(programs2).ids() %}
  
  {% for id in program2Ids %}
      {% if id not in programIds %}
          {% set programIds = programIds|merge(id) %}
      {% endif %}
  {% endfor %}
  
  {% set programEntries = craft.entries.section('news').id(programIds).order(dateUpdated).limit(4) %}
  
  {% for entry in programEntries %}
      {{ entry.title }}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: This looks like a great way to go. I'll let you know how it goes when I implement later today.  
  > **username_0**: This worked almost perfectly. I had to remove the "order(dateUpdated).limit(4)" from the two relatedTo queries due to a 503 error: "CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'dateUpdated' in order clause is ambiguous"  
  > **username_1**: Glad it worked. I was trying to save some processing power by limiting the initial results, but it's possible that 'order' and 'limit' is not compatible with `relatedTo()` and/or `ids()` (or maybe the order that they are chained needs to switch—not sure). Regardless, I'll remove it from the answer.  
  
  ---
  
  