Sort & filter entries from a structure field

Now I try to have a parent page to show the calendars of its children pages, adding the dates from one page to the other, sorting in ascending order (with title). I use the code you provides and try my best to make it work for my new purpose, without success. Can somebody point me in the right direction, thx

<?php foreach ($page->children() as $subpage):?>

<?php if($calendars = $subpage->calendriers()->toStructure()->filterBy('eventdate', '>=', date('Y-m-d'))->sortBy('eventdate', 'asc');
foreach ($calendars as $calendar): ?>
<ul>
<?= $calendar->eventdate()->toDate('d F') ?> <?= $subpage->title() ?>
</ul>
<?php endif ?>
<?php endforeach ?>

Apart from the fact that the if statement doesn’t make sense because if will always return true (a collection is always a collection even if it doesn’t have items) and the closing parenthesis is missing: what exactly doesn’t work as expected? Currently, you are only sorting the events of each subpage.

The if statement should look like this:

<?php foreach ($page->children() as $subpage):?>

  <?php $calendars = $subpage->calendriers()->toStructure()->filterBy('eventdate', '>=', date('Y-m-d'))->sortBy('eventdate', 'asc'); ?>
  <?php if ($calendars->isNotEmpty()):  ?>
    <?php foreach ($calendars as $calendar): ?>
    <ul>
      <?= $calendar->eventdate()->toDate('d F') ?> <?= $subpage->title() ?>
    </ul>
  <?php endif ?>
<?php endforeach ?>

But if you want to sort all events of all subpages by date, you have to first put them all into one collection.

1 Like

OK I will read how to do one collection now.
For the php, it seems like I’m still stuck in a kind of magical thinking…
Thank you for your patience.