At the bottom of an entry I want to display links for eight other posts. To make this useful, what I'd like to do is:

  • start with entries that are related to the post currently being viewed by category (generally there are 4-5 of these)

  • then fill in the remain slots with the other posts (not related by category), newest first

Is there a simple-ish way to do this? I started down the path of separate queries, counting, and merging results but something about that feels more complicated that it should be. I'm wondering if this can be achieved via ordering or grouping?

Thanks in advance.


1 Answer 1


This is probably pretty much the same code you already have, but I can't think of a more elegant solution:

{% set relatedEntriesIds = craft.entries.section('news').relatedTo(entry).limit(8).ids() %}

{% if relatedEntriesIds|length < 8 %}
    {% set moreRelatedEntriesIds = craft.entries.section('news').limit(8 - relatedEntries|length).ids() %}
    {% set relatedEntriesIds = relatedEntriesIds|merge([moreRelatedEntriesIds]) %}
{% endif %}

{% set relatedEntries = craft.entries.id(relatedEntriesIds).fixedOrder(true) %}
  • Yeah, I think this is about as clean as it gets, and a good solution. It's also a nice example of something that is probably better to handle with PHP in a custom business logic plugin. Twig gets convoluted fairly quickly, even for something relatively simple as this. Jun 9, 2015 at 14:32
  • Do you think it's possible to reduce the amount of DB queries with PHP / make performance better, @mmikkel? You'd still have these criteria models, or is something "complex" like this possible with custom DbCommands and a single query?
    – carlcs
    Jun 9, 2015 at 14:42
  • Probably possible to do with one query, but also probably not worth the effort :) Possible performance gains aside, doing it in a plugin would make for a much cleaner template and more readable, isolated logic – both big wins in my book – even if the PHP solution would more or less "mirror" the Twig above. Preference is also relevant here, of course. Personally I find complex Twig pretty hard to read (especially when/if it bleeds into the markup). Jun 9, 2015 at 15:08
  • Looks like we're all ending up in the same place… I'll keep running with this approach. Thanks.
    – philzelnar
    Jun 9, 2015 at 15:49

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.