Sum & count from grandchildren structure fields

I really donโ€™t know which is better, Kirby itself or your support on the forum. :pray:

Collecting in the structure was the most straightforward solution, and it works great. :sunglasses:

Here is the solution, if someone needs it in the future:

Created plugin as instructed in this thread, and created foreach loop with additional structure collection

<?php foreach ($page->children()->published()->sortBy('title', 'desc') as $season) :

        // fetch a collection of pages https://forum.getkirby.com/t/sum-count-from-grandchildren-structure-fields/23595
        $eventCollection = $season->children()->listed();

        // pass the collection of pages and the name of the field to the function https://k2.getkirby.com/docs/cookbook/the-structure-field#merging-structure-fields
        $entries = createNewStructure($eventCollection, 'races');

        // filter races
        $gallopRaces = $entries->filterBy('raceType', 'gallop');
        $trotRaces = $entries->filterBy('raceType', 'trot');
        $hurdleRaces = $entries->filterBy('raceType', 'hurdle');

        // count races
        $totalRaces = $entries->count();
        $totalGallop = $gallopRaces->count();
        $totalTrot = $trotRaces->count();
        $totalHurdle = $hurdleRaces->count();

    ?>

<?= $totalRaces ?>
<?= $totalGallop ?>
...

<?php endforeach ?>