2

If we pass through an array of Channels & Structure sections we find that Channels entries tend to be listed first. So the first 15 will comprise of ChannelOne and ChannelTwo and then start listing the Structure section.

{% set homepageEntries = craft.entries({
    section: ['channelOne','StructureOne',channelTwo','StructureTwo'],
    limit: 15
}) %}

{% for item in homepageEntries %}
{{ item.title }}
{% endfor %}

All we want is a list of the 15 most recent items from a number of sections.

It does not look to be as straightforward as it seems. Creating multiple arrays of ids and merging still seems to produce the same results.

3

I was presuming that the default Order for Channels and Structure was postDate desc. But it turns out the default for Structure is sortOrder

{% set homepageEntries = craft.entries({
    section: ['channelOne','StructureOne',channelTwo','StructureTwo'],
    order: 'postDate desc',
    limit: 15
}) %}

Explicitly stating the order seems to fix this issue

2

I'm not exactly sure why what you're trying doesn't work, but there seems to be a workable solution in this post: How do I sort a collection of entries from multiple sections by date?.

For your example:

{% set channelOneIds = craft.entries.section('channelOne').ids() %}
{% set structureOneIds = craft.entries.section('structureOne').ids() %}
{% set channelTwoIds = craft.entries.section('channelTwo').ids() %}
{% set structureTwoIds = craft.entries.section('structureTwo').ids() %}

{% set homepageEntryIds = channelOneIds
    |merge(structureOneIds)
    |merge(channelTwoIds)
    |merge(structureTwoIds)
%}

{% set homepageEntries = craft.entries.id(homepageEntryIds).limit(15) %}

{% for item in homepageEntries %}
    {{ item.title }}
{% endfor %}

The sorting by postDate descending, is the default, but if you want to be explicit, you can add .order('postDate desc') just before the .limit when setting homepageEntries.

  • Thanks Jake. Yes I had tried that one but it doesn't look very efficient where we have 1000s of entries in each section of which there are 6 +. Im real curious though as to why this doesn't work out of the box as seems like a simple thing that should 'just work'. – John Henry Donovan Aug 27 '17 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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