Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Some notes here~
{% if relatedArticlesSelect | length == 3 %}
{% set relatedArticles = entry.relatedArticlesSelect.limit(3).all() %}
is the same as
{% set relatedArticlesSelect = entry.relatedArticlesSelect.all() %}
if you know there are only going to be three elements. dot all will fetch all of the elements attached
This is how I would code it ~
{% set selectedEntries = entry.relatedArticlesSelect %}
{% set existingEntries = ['not']|merge(selectedEntries.ids()) %}
{% set relatedEntries = craft.entries.section('articles').relatedTo(mainCategory).id(existingEntries).limit(3 - selectedEntries|length).all() %}
{% set allEntries = selectedEntries | merge(relatedEntries) %}
I believe that the sort order is post date desc by default, it might be asc.
This will hide elements that have already been output
This also assumes that the selected entries field is limited to three entries
Let me know if you have questions

This comment has been minimized.

Copy link

@michtio michtio commented Mar 12, 2020


In your example if there are 3 selectedEntries it will never output anything. There still needs to be a merge of the selectedEntries en relatedEntries. Just to let you know if you want to ever update this gist! :)


This comment has been minimized.

Copy link
Owner Author

@clarknelson clarknelson commented Mar 12, 2020

Thanks, i've added an allEntries variable so it is more clear :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment