3

I want to select all children of all entries that fulfil specified relation parameter.

I have tried this, but I think .isDescendantOf only supports single entry. Is this even possible?

{% set parents = craft.entries.section('training').level('1').relatedTo(relationParam) %}
{% set children= craft.entries.section('training').isDescendantOf(parents).eventDate('>= ' ~ now).order('startDate DESC') %}

I thought giving more background to this might help, as possibly an entirely new approach will be necessary. Consider this setup in Craft CMS enter image description here I have a 2 level structure, where top level is a big matrix describing an event and has quite a big classification section where multiple categories are selected. Then it's children are specific instances of that event with their own date and location.

I don't want to force my client to repeat categorization on each of the lvl2 entries since they've already done it on lvl1.

Can I select all lvl2 entries that have parents fulfilling specific relation param?

  • 1
    Would you mind using php for that? It's relatively easy with php (twig filters) but it will become really nasty with twig – Robin Schambach Feb 8 '18 at 19:43
2

Yes, it looks like the descendantOf param doesn’t accept an array of element models or array of element IDs. Here’s what you can do in Twig to work around the problem.

This solution does trigger an additional query for every parent entry matching your relatedTo criteria. But as it’s only fetching the ids column, it might not perform too bad actually, depends on the amount of matching parent entries though.

{% set idsParam = [] %}
{% for parent in parents %}
    {% set idsParam = idsParam|merge(parent.getDescendants().ids()) %}
{% endfor %}

{% set children = craft.entries({
    section: 'training',
    ids: idsParam,
    eventDate: '>= ' ~ now|date('U'),
    order: 'startDate DESC',
}) %}

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.