I my template I need to list some itens from a collection in descending order (past items) and some other items in ascending order (future items)… I’m using filterBy() and sortBy() to do so… and all is ok, but since I end up with two separate collections it gets impossible to paginate correctly.
I there a way to do some sort of “collection concatenation”?
Maybe there’s other solution I’m not aware of.
Some of the code I’ve mentioned above:
$filter_next = $page->children()->visible()->filterBy('activity_day', '>=', date('Y-m-d') )->sortBy('activity_day', 'asc');
$filter_past = $page->children()->visible()->filterBy('activity_day', '<', date('Y-m-d') )->sortBy('activity_day', 'desc');
This sounds like a merge case !
https://getkirby.com/docs/cheatsheet/pages/merge
<?php
$filter_next = $page->children()->visible()->filterBy('activity_day', '>=', date('Y-m-d') )->sortBy('activity_day', 'asc');
$filter_past = $page->children()->visible()->filterBy('activity_day', '<', date('Y-m-d') )->sortBy('activity_day', 'desc');
$filter_all = $filter_next->merge(filter_past);
?>
Does that work for you ?
Thank you @Thiousi
it almost worked… the new merged collection is somehow re-sorted and doesn’t maintains the merging order, a no-go for this use case… any other suggestion?
Oops… my bad!
It was actually freaking perfect!!
Thank you @Thiousi
1 Like
Weird. The merge function should work. Another way to this with PHP would be:
$filter_all = new Pages(array($filter_next, $filter_past));
1 Like
Oh, just saw this update. Good to know it works.
I’m glad it worked out perfect @rhawbert!
I’ve marked this as solved.