# Filter Publications by Structured Field with multiple Date values

**URL:** <https://forum.getkirby.com/t/filter-publications-by-structured-field-with-multiple-date-values/32463>\
**Category:** Questions\
**Tags:** v4\
**Created:** [September 12, 2024, 8:28am UTC](https://forum.getkirby.com/t/filter-publications-by-structured-field-with-multiple-date-values/32463 "2024-09-12T08:28:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rapha](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rapha/32/6376_2.png) [@rapha](https://forum.getkirby.com/u/rapha)\
**Post date:** [September 12, 2024, 8:28am UTC](https://forum.getkirby.com/t/filter-publications-by-structured-field-with-multiple-date-values/32463/1 "2024-09-12T08:28:28Z")

</div>

Hi all,

I try to filter and sorting Publications/Events from a Structured field (eventdates) value named eventdate. this is part of my controller code in which i get all publications and try to filter the events out.

**Controller Snippet**

```auto
    $eventFilter = 'Veranstaltungen';
    $allEvents = $publications->filterBy('datafilter', $eventFilter);

    $events = $allEvents->filter(function ($child) {
        return $child->eventDates()->toStructure()->filter(function ($item) {
            return $item->eventdate()->toDate() >= time();
        });
    })->limit(3);

```

**Blueprint**

```auto
      eventDates:
        label: Veranstaltungs Daten
        type: structure
        sortBy: eventDate desc
        fields:
          eventDate:
            label: Datum
            type: date
            width: 1/3
            display: dd.mm.yyyy
          startTime:
            label: Startzeit
            type: time
            width: 1/3
          endTime:
            label: Endzeit
            type: time
            width: 1/3

```

Events have multiple Dates (eventdate) stored in Structured field.  
I want to achief getting the nearest 3 future events from today.

thank you for your hints.

cheers raphael

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [September 12, 2024, 10:06am UTC](https://forum.getkirby.com/t/filter-publications-by-structured-field-with-multiple-date-values/32463/2 "2024-09-12T10:06:31Z")

</div>

You would have to fetch all items from all pages into one structure collection first. Then do the filtering on that collection.

---

<div class="post-metadata">

**Author:** ![rapha](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rapha/32/6376_2.png) [@rapha](https://forum.getkirby.com/u/rapha)\
**Post date:** [September 12, 2024, 1:24pm UTC](https://forum.getkirby.com/t/filter-publications-by-structured-field-with-multiple-date-values/32463/3 "2024-09-12T13:24:27Z")

</div>

thank you @texnixe  
sorry for the silly question but how do i make a structure collection? I just know the “normal” collections under **site/collections**.

---

<div class="post-metadata">

**Author:** ![rapha](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rapha/32/6376_2.png) [@rapha](https://forum.getkirby.com/u/rapha)\
**Post date:** [September 12, 2024, 1:57pm UTC](https://forum.getkirby.com/t/filter-publications-by-structured-field-with-multiple-date-values/32463/4 "2024-09-12T13:57:48Z")

</div>

i am not sure but i think i got it with:

```auto
    $allEvents = $publicationsPage->children()->filterBy('datafilter', 'Veranstaltungen');

    $allEntries = new Structure();
    foreach ($allEvents as $event) {
        $allEntries->add($event->eventDates()->toStructure());
    }

    $upcoming = $allEntries->filter(function ($child) {
        return $child->eventdate()->toDate() > time();
    });

    $upcoming->sortBy('eventdate', 'desc');

```

i get now:

```auto
  0 => Kirby\Cms\StructureObject {#1957 ▶}
  1 => Kirby\Cms\StructureObject {#1959 ▶}
  2 => Kirby\Cms\StructureObject {#1961 ▼
    #field: Kirby\Content\Field {#1955 ▶}
    #id: "2"
    #params: array:10 [▶]
    #parent: Kirby\Cms\Page {#414 ▶}
    #siblings: Kirby\Cms\Structure {#1956}
    #content: Kirby\Content\Content {#1962 ▶}

```

how do i access the content of these?

---

<div class="post-metadata">

**Author:** ![rapha](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rapha/32/6376_2.png) [@rapha](https://forum.getkirby.com/u/rapha)\
**Post date:** [September 12, 2024, 3:01pm UTC](https://forum.getkirby.com/t/filter-publications-by-structured-field-with-multiple-date-values/32463/5 "2024-09-12T15:01:05Z")

</div>

Ok my final collections is like:

```auto
<?php

use \Kirby\Cms\Structure;

return function () {

    $publicationsPage = site()->sitedefaults()->toObject()->publicationPage()->toPage();
    $allEvents = $publicationsPage->children()->filterBy('datafilter', 'Veranstaltungen');

    $allEntries = new Structure();
    foreach ($allEvents as $event) {
        $allEntries->add($event->eventDates()->toStructure());
    }
    $upcoming = $allEntries->filter(function ($child) {
        return $child->eventdate()->toDate() > time();
    })->sortBy('eventdate', 'asc')->limit(3);

    return $upcoming;
};

```

and this seams to work very well. get the 3 next upcoming events.
