1

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.

1 Answer 1

3

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 %}
3
  • This looks like a great way to go. I'll let you know how it goes when I implement later today.
    – Paulo
    May 1, 2015 at 18:44
  • 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"
    – Paulo
    May 1, 2015 at 22:19
  • 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. May 1, 2015 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.