Skip to content

Instantly share code, notes, and snippets.

@clarknelson
Last active March 12, 2020 18:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarknelson/67b08e99e858580477ba8da0ae304d99 to your computer and use it in GitHub Desktop.
Save clarknelson/67b08e99e858580477ba8da0ae304d99 to your computer and use it in GitHub Desktop.
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
@michtio
Copy link

michtio commented Mar 12, 2020

@clarknelson

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! :)

@clarknelson
Copy link
Author

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