Filter interval of pages

I want to get a result of an interval of pages. Look at this:

My
long
list
of // Current page
all
the
pages

I want to use the current page as a pointer and show all the pages after that.

This is the collection that I want:

all
the
pages

I did not get very far…

$items = $page->siblings()->sortBy('slug', 'asc')->filter(function($child) {
	echo $page->slug(); // error
});

In my mind I need to have access to the $page object to figure out where to set the “pointer”.

Are those pages sorted by some particular order?
Because if it’s just a normal order you can use a simple filterBy I think. So something like this maybe?

$uid = $page->uid();
$items = $page->siblings()->filterBy('uid', ">" , $uid)
1 Like

Yes, it does indeed work, thanks! :slight_smile:

I do some filtering and sorting before this, but because I get a collection back from it I can use your function on that collection.

Great!

1 Like

Just as a complement I wanted it to start over if the collection was on the last item.

Here is my final solution:

$limit = 5;
$collection_filtered = $page->siblings()->sortBy('slug', 'asc')->filter(function($child) {
  return true; // Some custom filtering
});

$collection_after = $collection_filtered->filterBy('uid', ">" , $page->uid())->limit($limit);
$collection_before = $collection_filtered->limit($limit - $collection_after->count());

$collection_merged = new Pages();
$collection_merged->add($collection_after);
$collection_merged->add($collection_before);

foreach($collection_merged as $item) {
  echo $item->title();
}

It takes the current page and get the 5 next items. If it’s at the end of the collection it will start over again. As long as the initial filtered collection is above 5 items it will always return 5 items.

Why?

I use it kind of as a gallery of related pages. What I think is nice with it is that every page have a unique set of related pages.

Is this for a blog or something? Where you give the user some related content at the end of the page?
Because if that’s the case, why not just display 5 items at random?

Here is an alternative way of getting the remaining items, no matter what you sort by:

<?php
  $collection = $page->siblings();
  $offset = $collection->indexOf($page) + 1;
  $remainder = $collection->offset($offset);
?>
2 Likes

@manuelmoreale

I’m not too fond of random stuff, in general. If the visitor comes back to the page to look for that related product and it’s random, it can be gone. With 3000 pages it could take a few F5 presses to get it back. :confused:

Also it’s not nice for an SEO point of view. For Google it can look like the content changes all the time and I don’t think it’s a good way to keep the pages flow the pagerank.

Therefor I like to keep things as “stable” as possible. :slight_smile:

@texnixe

Nice! I’ll think I’ll use that instead. :slight_smile:

Fair point.
I was suggesting the random approach because when you say…

What I think is nice with it is that every page have a unique set of related pages.

…that’s not exactly true.

Suppose you have 10 pages.
If I’m looking at page number 3 you then show me pages 4 to 8.
If I then open page 4 you show me pages 5 to 9 and that means that 80% of the suggestions are identical to the one displayed in the previous page.

Yeah, yeah. :slight_smile:

I was refering to the related pages as a group. No group matches another group exactly.

But if you are looking at individual items and compare it like that you are right of course. :slight_smile:

1 Like

why not sort by slug and then create a pagination? it allows for a a single element or up to a range of n-items to be grouped.

Yes, that might be a good idea as well. Thanks! :slight_smile:

I have now added it here: https://github.com/jenstornell/kirby-secrets/wiki/Advanced-collections

If it’s possible to improve the code even more it would be nice. If it’s not possible, it’s fine anyway. It’s working great and that’s the most important thing.