Show scheduled posts in panel

Is it possible to filter a page section by date? I want to show only the the pages that are published, but with a date in the future.

Possibly with the query language, since you can do the equivalent of pretty much anything you can do with the php api… howeverrr… ive never tried with a date, but you can certainly filter on the field. You might have to do a custom method, since you need to know what time it is.

https://getkirby.com/docs/guide/blueprints/query-language

I tried using the query option, but i guess it does not apply to the pages section:

  query: site.children.listed.template('note') # get all children with the note template

I dont think thats enough, you need to filter it…off the top of my head…

query: site.children.listed.filterby('template','note')

You may have to use double qoutes rather then singles.

ok this plugin let’s me filter :https://github.com/rasteiner/k3-pagesdisplay-section

but I don’t see how I can filter by date with it as I need to return the current time

Well I think you can do it with the query language, you just need to make a custom method that gets the current time i think. wether that plugin will react to that too, i dont know. Looks like it might.

1 Like

The solution is to use the pages display plugin linked above.

Then create a plugin with:

Kirby::plugin('my/pagesMethods', [
'pagesMethods' => [
    'future' => function () {
        return $this->filterBy('date', '>=', date('Y-m-d'));
    }
]

]);

And use like:

query: page.children.future

Good stuff, but didnt you want future posts? '>=' will catch pages with todays date. I thought you wanted from tomorrow onwards, which would just be >.

hmm… yes really I need to use the time as well as the site has multiple posts a day.

return $this->filterBy('date', '>', time());

thanks @jimbobrjames but that throws an error

Object of class Kirby\Cms\Field could not be converted to int

My bad… try this…

'future' => function () {
  $datetime = new DateTime();
  return $this->filterBy('date', '>=', $datetime->format(DateTime::ATOM));
}

You may have to convert your field date to a full and proper time stamp that looks like:

2019-05-16T15:18:55+01:00

I don’t think you can convert the date field to show a full timestamp ?

My dates look like this:
2019-05-16 12:50

but in the docs there is no option to change the output.

thanks for all your help btw :grinning:

I dont mean the stored value…to properly filter and compare on it, that date can be turned to ISO with…

$datetime = new DateTime('2019-05-16 12:50');
echo $datetime->format(DateTime::ATOM);

But im not sure how to do that within the pages method function… im not sure even can see it needs to be a page method i think to work with the field… my brain is working…

@texnixe can you please throw us a rope here… :slight_smile:

'future' => function () {
   return $this->filter(function($child) {
      return $child->date()->toDate() > time();
    });
}
1 Like

thanks @texnixe that throws a 500 error, is $child available here?

Sorry, too fast, there was the closing parenthesis missing, I corrected it above.

ok working great now! thanks so much for both of your help :smile: