Skip to content

Eager-load related elements (across all sites) on auto-injected entry`s template #11393

Answered by brandonkelly
richhayler asked this question in Q&A
Discussion options

You must be logged in to vote

Relational fields will query for related elements in the same site as the source element by default (or in the Target Site, if that setting is set), whether you’re eager-loading or not, and in either case you can change that behavior by overriding the site/siteId param.

Without eager-loading:

{% set relatedEntries = entry.myEntriesField
  .site('*')
  .collect() %}

With eager-loading:

{% set sourceEntries = craft.entries()
  .section('foo')
  .with([
    ['myEntriesField', {site: '*'}],
  ])
  .collect() %}

{% for sourceEntry in sourceEntries %}
  {% set relatedEntries = sourceEntry.myEntriesField.collect() %}
  ...
{% endfor %}

Note that there is almost never a good reason to call craft…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@richhayler
Comment options

@brandonkelly
Comment options

Answer selected by brandonkelly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants