1

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'?

1

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) %}

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.