I got a section called news which contain 2 types of entries: news and press.

Those entries are linked with 2 different categories :
news entries have category newsType
and press entries have category pressType.

In twig template I want to make 2 entries listing:

  1. one from news entries and category newstype = A
  2. the other from press entries and category presstype = X

Then merge those 2 lists to display them ordered by publication date?


You can probably use the relatedTo parameter with multiple relation criteria:

{% set entries = craft.entries.section(['news', 'press']).relatedTo('or',
    { targetElement: newsTypeCategory, field: 'newsTypeCategoryField' },
    { targetElement: pressTypeCategory, field: 'pressTypeCategoryField' }).order('postDate desc') %}

Note that newsTypeCategory and pressTypeCategory should be category IDs (or CategoryModels) so you might need to pull the relevant categories as well:

{% set newsTypeCategory = craft.categories.group('newstype').slug('someslug') %}

Also note that you'll obviously need to change the field values to the real handles for your Categories fields, in the example criteria above.

An alternative approach is to use the SuperSort plugin to sort two arrays of entries on the postDate attribute:

{% set newsEntries = craft.entries.section('news').relatedTo(newsTypeCategory) %}
{% set pressEntries = craft.entries.section('press').relatedTo(pressTypeCategory) %}
{% set allEntries = newsEntries|merge(pressEntries)|supersort('sortAs', '{ postDate }') %}

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.