Matthew Fitzsimmons

Kirby Plugin to Pull Latest Non-Future Items

I've written a Kirby plugin to pull a specified number of posts either from a specific category, tag, or just in general that are the latest ones excluding any posts that are in the future. The date format is set for my blog post date format, but you can modify it to fit your needs.

Use either a 'category' or 'tag' option to specify a category or tag, and use a 'num' option to specify how many posts to pull, defaulting to only one (giving you the latest, non-future item).

It expects your category field to be called categories, and your tag field to be called tags.

It returns results in what I consider the cleanest way: if you tell it to only return a single item, it returns that item (not an array) so you don't have to loop through to get the one item. If you request it to return multiple items, it will return an array with those items.

Unlike some plugins, you pass it a set of articles, not a parent page. The reason for this is you can apply additional filtering on the set of articles first and then pass it to the plugin for the rest of the procedure.

For example, you can exclude some categories and then pass your articles off to the plugin for further processing like this:

     <? $articles = $page->children()->visible()->filterBy('categories','!=','Category 1',',')->filterBy('categories','!=','Category 2',',') ?>
<? $article = latestnotfuture($articles); ?>

The above code sample will pull the latest non-future article that isn't in 'Category 1' or 'Category 2' but include all other categories. You can then treat the $article object the same as any other Kirby object.

    <? $featured = $page->children()->visible()->filterBy('featured','on') ?>
<? $featured = latestnotfuture($featured, array('num'=>'2')) ?>

This code sample will pull the latest two non-future articles that are marked as featured. Notice you can include whatever criteria you want in a filter before you pass it on to the plugin which strips out any future posts. This one will return an array, because you're requesting more than one item, so you'd use a standard foreach loop to pull out the items and treat them as Kirby objects.

You can find the plugin in this github gist. I hope someone besides me finds this useful.