Icon for post type: learned Merging Kirby Collections Permalink: Merging Kirby Collections Bookmark Icon

After yesterday’s post on Digital Gardens and how to possibly arrange information, posts and all that in a better or more interesting and fun way, I started to try a few things. One of the approaches is to not list articles, notes and other things separately anymore, but instead list them all together, maybe with a marker for each type.

While trying to figure out how to best achieve this, I came across two helpful ways on how to do it in Kirby:

Option 1: $pages->add() which lets you add additional pages or collections to collections;

Option 2: $pages->merge() which merges one collection with one or more other ones.

I'm not even sure what the exact difference between the two methods is, since they do seem quite similar. I opted for $pages->merge() and the code in progress currently looks like this and works pretty nicely:

$articles = page( 'articles' )->children()->listed()->flip();
$notes = page( 'notes' )->children()->listed()->flip();
$tilrs = page( 'tilrs' )->children()->listed()->flip();

$entries = $articles->merge( $notes, $tilrs );

$entries = $entries->sortBy( function ( $entries ) {
    return $entries->date()->toDate();
}, 'desc' );

Let’s see where this little experiment will take me…

Update:
According to Bastian’s comments below, I have changed the code to his suggested solution.

$articles = page( 'articles' )->children()->listed()->flip();
$notes = page( 'notes' )->children()->listed()->flip();
$tilrs = page( 'tilrs' )->children()->listed()->flip();

$entries = $articles->merge( $notes, $tilrs );
$entries = $entries->sortBy( 'date', 'desc' );

Leave a comment

Replied on your own website? Send a webmention!

Comments