Section filters

It’s nice that section filters are coming to kirby 4!

I tried to filter pages by the current date with the help of a page method or page model. But it doesn’t seem to work.

query: site.childrenAndDrafts.filter('dateStart', '>=', '{{ page.now }}')

It works perfectly with a string, f.ex.

query: site.childrenAndDrafts.filter('dateStart', '>=', '2023-09-28')

Are just static values allowed like in the example on the website, or is there something I missed?

Is that your model method?

Yes, page.now is the model in this case. But I tried it also with a method.

The model looks like this:

class ExhibitionPage extends Page {
    public function now()
    {
        return date('Y-m-d');
    }
}

Both method or model works fine if I want to display it in the list item like info: '{{ page.now }}'

To compare a date value, try using date >= as comparison operator

query: site.childrenAndDrafts.filter('dateStart', 'date >=', '{{ page.now }}')

Sadly, it doesn’t work…

A workaround would possibly be a programmable blueprint, right?

Or should the way I tried work in theory?

I had the same issue today and could only fix it with a programmable blueprint. The comparison operator 'date >=' works like expected then.

Two things:

  1. You are passing the string '{{ page.now }}' to the filter method. The whole line already is a query, there’s no further template “substitution” happening ('{{ page.now }}' simply stays '{{ page.now }}'). You would probably want to write this as

    query: site.childrenAndDrafts.filter('dateStart', 'date >=', page.now)
    
  2. if you just want to compare the date with “now”; it’s interesting to know that, in PHP, stuff like “now”, “today”, “tomorrow”, “Tuesday next week” or “first day of January this year” are actually all valid date strings. You wouldn’t therefore need a model function, it’s enough to write

    query: site.childrenAndDrafts.filter('dateStart', 'date >=', 'today')
    

    To have the same effect.

2 Likes

Thanks for the explanation. You are totally right!

Oh, nice! Didn’t try out today. That definitely works!

Thanks!