Exclude list items based on date?

  • Hi,


    I want to exclude any event items that are older that $now, so only events that are happening now or in the future are displayed.


    The value of event_date_upcoming is different for each item in the perch_collection


    Does this look like it should work?



    event_date_upcoming is generated via a callback, would that have any impact on this working?


    :/

  • Looks like Perch can't use an ID generated from each as a filter value? Is that correct?


    I did this as a test:

    Code
    1. perch_collection('Events', [
    2. 'template' => '_events/events--listing.html',
    3. 'filter' => 'postDateTime', // Date item was created 2020-02-20 14:54:00
    4. 'match' => 'eq',
    5. 'value' => '2020-02-20 14:54:00',
    6. 'each' => function ($item) {
    7. ...
    8. return $item;
    9. },
    10. ]);

    postDateTime is a timestamp for when the item was created, and the value 2020-02-20 14:54:00 is just hardcoded in for now, as I know it's the same as postDateTime  . I just wanted to see if it'd work... And it does. Only the item that matches the filter/value is shown.


    So.... Is there a way to get Perch (Runway) to use an ID generated in each as part of filter/value?

  • So.... Is there a way to get Perch (Runway) to use an ID generated in each as part of filter/value?

    No, it is not.

    • Any additional IDs you add to the item in this context is not in the database so Perch can't use it in its database queries
    • The each callback function is executed after Perch queries the database


    How are you determining what the upcoming date is? Is it stored in a date field?

  • Ah, yes that makes sense.


    I just hoped I was missing something.... I was thrown by event_date_upcoming, it was showing up when I used perch:showall.


    event_date_upcoming is generated using an each callback function, the dates are in a perch:repeater.


    The setup I've got is, each item in the collection can have multiple dates, which are set in a perch:repeater. I'm using the each callback function to change the order of the dates into reverse chronological order. As I can't be sure the admin user will always enter multiple dates in the correct order.


    I was hoping to use event_date_upcoming to filter the collection, but as you pointed out the "each callback function is executed after Perch queries the database"


    Which is fine, I guess. I'll just add a date field in the template and request admin to set the last date of an event manually (so it's in the database). I'll then be able to filter on that. It'll work but it's an extra (small) step for the admin - they all add up :)


    Thanks for confirming how it works, it's good to know.