3

I have a scenario where level 1 structure entries each have several to tens of level 2 child entries, and several to tens of entries in a different channel which are also allowed to be related to the level 1 structure entry.

'events' structure
  - event entry 'e'
    - child 1
    - child n
'articles' channel
  - article 1
  - article n: 
      could be related to event 'e'
'people' channel
  - person 1 
  - person n: 
      could be related to a child of 'e' via a Matrix field
      and/or an article entry related to 'e' via a Matrix field

For each of the child entries and the article entries, there are Matrix fields in which one or more people entries can be related.

Other than looping through the set of children of 'e' and the set of related articles, and then through each of their Matrix rows to fetch their corresponding people entries, is there a cleverly more efficient way to get all the people entries used for event 'e'?


Update (revised)

Thanks, Ben, for pointing the way. Initially I couldn't get this to work but then I spotted my error. Here’s what now works a treat for the following specifics…

I have my child pages as sessions where:

  • Each child session has a Matrix field called eventProgramme
    • There are two Entries fields defined in this Matrix, one called speakers and one called chairs.

This works:

{% set people = craft.entries.section('people').relatedTo({
    sourceElement: sessions,
    field: 'eventProgramme'
})
.all() %}
  • It's cool that sourceElement can be an array such as sessions.
  • If I want to target a specific field I can just use something like field: 'eventProgramme.speakers'

One thing I don't understand is the distinction between targetElement and sourceElement. Only the latter works in this context.

Update 2

Doh! The difference between source and target element is clearly explained in the Docs but I had missed it:

  • "The source has the relational field where other elements are chosen."
  • "The target element is the one selected by the source"
3

If you are trying to get all people entries related to the event entry itself (what you call event "e") then I would suggest going through matrix as follows.

{% set people = craft.entries.section('people').relatedTo([
    'or',
    { targetElement: event, field: 'matrixFieldHandle.relatedEvents' },
    { targetElement: event, field: 'matrixFieldHandle.relatedArticles' }
]) %}

If you need the target element to be all children of the event then this might do the trick.

{% set eventChildren = event.children %}

{% set people = craft.entries.section('people').relatedTo([
    'or',
    { targetElement: eventChildren, field: 'matrixFieldHandle.relatedEvents' },
    { targetElement: eventChildren, field: 'matrixFieldHandle.relatedArticles' }
]) %}

You can also combine the event with its children, if necessary, as follows.

{% set eventWithChildren = event.children|push(event) %}
1
  • Thanks Ben. That’s super helpful. See my Update above. May 14 at 14:27

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.