Previous and Next with FilterBy

Currently I’m struggeling with a small problem. My customer wants a next/previous button in each article. No big deal, but the problem is, that when the user has in the overview clicked a filter, it should only prev/next the pages with that active filter.

In this usecase, the “next” page should be one where the filter is found in the field “arbeitsschritte”. I’m not sure how I can manage this. Anybody a idea? (I know the code below doesn’t work, it’s a bit of example)

<?php if ( get('filter-arbeitsschritt') ) : ?>
  <?php $filtered = $page->siblings()->filterBy('arbeitsschritte', get('filter-arbeitsschritt'), ','); ?>
  <?php if ( $filtered->hasNext() ) : ?>
    <a href="<?php echo $page->next()->url(); ?>?filter-arbeitsschritt=<?php echo get('filter-arbeitsschritt'); ?>">Next</a>
  <?php endif; ?>
<?php endif; ?>

Next and prev don’t work with filtered collections, you can use the getNext() and getPrev() methods from my repo here:

Usage: Sorting nextListed

I have done a few tests but can’t get it to work … have you a example for me? Based on this. I want the next URL but filtered if the next page has in the field “arbeitsschritte” the value “x”

<?php if ( $page->hasPrev() ) : ?>
  <a href="<?php echo $page->prev()->url(); ?>"><i class="fal fa-angle-left"></i></a>
<?php endif; ?>
<?php if ( $page->hasNext() ) : ?>
  <a href="<?php echo $page->next()->url(); ?>"><i class="fal fa-angle-right"></i></a>
<?php endif; ?>
<?php $collection = $page->siblings()->filterBy('arbeitsschritte', 'x') ?>

<?php if ($collection->isNotEmpty()) : ?>
  <?php if ($prevPage = $page->getPrev($collection)) : ?>
      <a href="<?= $prevPage->url() ?>"><?= $prevPage->title()->html() ?></a>
  <?php endif ?>

  <?php if ($nextPage = $page->getNext($collection)) : ?>
      <a href="<?= $nextPage->url() ?>"><?= $nextPage->title()->html() ?></a>
  <?php endif ?>
<?php endif ?>