Next page sort by custom fields

Is it possible to get the next page sorted by a custom field?

e.g.
$page->nextVisible()->sortBy('stardate', 'asc')

thanks for help

The “next page” in Kirby’s terms is always the one that follows next in the file system.

What exactly do you want to return? The page that has a value in the stardate field that follows after the value of the current page’s stardate field?

Yes exactly, it is an event-page an I want to iterate the events by the startdate (type = date).

  startdate:
    label: Termin/Start
    type: date
    width: 1/4
  enddate:
    label: Ende
    type: date
    width: 1/4

You can iterate through a pages collection like this:

foreach($pages->visible()->sortBy('startdate', 'desc') {
  //do stuff
}

Or to get the children of a particular page:

foreach(page('events-page')->children()->visible()->sortBy('startdate', 'desc') {
  //do stuff
}

So then you need something like this if you want to get the next sibling of the current page:

$events = $page->siblings()->sortBy('startdate', 'asc');
$index  = $events->indexOf($page);
$next   = $events->nth($index + 1);
$prev   = $events->nth($index - 1);

Not tested though, so please let me know if that works.

If you want to iterate (e.g. on the event overview page), you need @texnixe’s solution.

3 Likes

This is how I did it for the events.php to show al events sorted by startdate(). But in the single page view (event.php) you see one event and by clicking the “next-button” I wanted to navigate to the next event, page by page, following by date…

Thanks a lot, you have been faster than I could answer to @texnixe .
$events = $page->siblings()->sortBy('startdate', 'asc'); $index = $events->indexOf($page); $next = $events->nth($index + 1); $prev = $events->nth($index - 1);

This works fine!

Great, thanks for confirming.

If you have a blueprint for the parent page of your events, you could set it to use one of the event pages’ date fields for its sorting:

num:
  mode: date
  field: startdate

This way, when you publish an event page (make it “visible”), the sort order will inherit from the specified field— it should also dictate how the nextVisible and prevVisible functions behave.

2 Likes

It’s even easier, the prev() and next() methods and their visible and invisible brothers and sisters take an optional sort parameter $page->prev('somefield'), e.g:

<nav class="nextprev cf" role="navigation">
   <?php if($prev = $page->prev('year')): ?>
      <a class="prev" href="<?php echo $prev->url() ?>">&larr; previous</a>
   <?php endif ?>
   <?php if($next = $page->next('year')): ?>
      <a class="next" href="<?php echo $next->url() ?>">next &rarr;</a>
   <?php endif ?>
</nav>

And yes, this great feature is missing from the docs; I’m gonna add that later today.

Edit: I have just added these option to the docs repo.

4 Likes

thanks @AugustMiller this is what was thinking first without knowing how to do (but I found it now in the docs), but there is also a page in the site where some featured events are displayed. Those are sorted manually…
thanks again for the note.

Yeah!, I expected that there is some build-in-method. It seems natural, that there could be a need for this. I am wondering I didn’t tried it out this way yet.
Thank you @texnixe, this is how I made it now and it works.