Eager-load related elements (across all sites) on auto-injected entry`s template #11393
-
Is there a way to eager-load related elements (entries field), across all sites for an auto-injected entry's template? Running the following code in an entry template to eager load the related entries just returns the items related to the current site. Is there a way I can eager-load the entry's relates entries across all sites or do I have to set this up seperately as shown below the first example?
Is the solution to remove
And then iterate over the array
https://github.com/craftcms/cms/blob/main/src/services/Elements.php#L2219-L2230 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
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 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 For example, if you need to fetch a related asset off of each of your related elements, you could eager-load them when first fetching the related entries: {% set relatedEntries = entry.myEntriesField
.site('*')
.with(['myAssetsField'])
.collect() %} |
Beta Was this translation helpful? Give feedback.
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:
With eager-loading:
Note that there is almost never a good reason to call
craft…