Filter next() and prev()


How can I use the next() and prev() or it´s brothers/sisters with the filterBy() method?

I want to check if the date and if it is in the future I don´t want the next() link to appear. Ideas?


My code…

My articles code from the site/templates/articles.php and it works perfectly.

          $articles = $pages->find('articles')
          ->filterBy('date', '<=', strtotime(date_default_timezone_get(date('Y-m-d'))))

I wish to add the equal to the filterBy() check here to the next and prev buttons. Is is possible?


Have a look at the custom methods here:


Yes. I tried this code. But it failed for me yesterday. Not sure if I added it correctly. How will you add this @texnixe? :blush:


You pass the filtered collection as an argument… optionally a sort parameter.



Do I understand you correctly if this how I need to go around this than?

$page->getNext(filterBy('date', '<=', strtotime(date_default_timezone_get(date('Y-m-d')))))

Will this work? But is this the way I call this in the template, right?

But the code over where do I apply it, or add it? What is the correct path?


Not quite, should be like this:

 $articles = $pages->find('articles')
          ->filterBy('date', '<=', strtotime(date_default_timezone_get(date('Y-m-d'))))

foreach($articles as $article):
  $next = $article->getNext($articles);
  $prev = $article->getPrev($articles);

As I said above, the method wants the filtered collection (in this case $articles) as a parameter.


Where do I place the custom methods?


In a plugin file, e.g a site/plugins/methods.php


Not sure if I get this strait yet. Sorry for being slow here. Trying really hard to understand this. :sweat:

So on my article.php page I will need to do this how, where I want my next and prev links?

With normal ways should be something like this:

<?php if($page->hasNextVisible()): ?>
  <div class="pull-right">
    <a href="<?php echo $page->nextVisible()->url() ?>"><?php echo $page->nextVisible()->title()->excerpt(20) ?></a>
<?php endif ?>

<?php if($page->hasPrevVisible()): ?>
  <div class="pull-left">
    <a href="<?php echo $page->prevVisible()->url() ?>"><?php echo $page->prevVisible()->title()->excerpt(20) ?></a>
<?php endif ?>

I am just unsure how to apply this code to get the wanted result. :rocket:


You have to define your $articles collection in the article.php controller or template as well, otherwise it will be undefined.

We can’t use the hasNextVisible() and hasPrevVisible() methods here or we would have create custom methods, but we don’t necessarily need them.

<?php if($next = $page->getNext($articles)): ?>
  <div class="pull-right">
    <a href="<?= $next->url() ?>"><?= $next->title()->excerpt(20) ?></a>
<?php endif ?>

<?php if($prev = $page->getPrev($articles)): ?>
  <div class="pull-left">
    <a href="<?= $prev->url() ?>"><?= $prev->title()->excerpt(20) ?></a>
<?php endif ?>

Since our collection already contains the visibility filter, we don’t have to apply it again.


Now I follow you. Thank you @texnixe. :smile::smile::smile:


Well, glad that it works now. :sweat_smile:


I still need to add it to the project. I now have an idea how to do this. :slight_smile:


Well, good luck! I’m here if you need me…


This worked out perfectly. Thank you for the help @texnixe. :smile: