Filter bug with pagination

Hello,

A weird thing is happening when I filter my events by category.

I have set up my agenda to show 24 events per page. But when I filter by a category the events stay on their original pages. Meaning that, after selecting a category, you can end up having no event on the first page, two on the second, 5 on the 4th, etc. I would like the events in the selected category to fill all the 24 slots available on the first page before overflowing on next pages.

I followed this tutorial for the filter: https://www.youtube.com/watch?v=ue15rQVQCqg&t=6s
And this for the pagination: https://www.youtube.com/watch?v=p1JhCpSRvlc

Here is my code:

<?php 

$filterBycategories = get ('filter');

$events = $page
    ->children()
    ->listed()
    ->flip()
    ->paginate(24)

    ->when($filterBycategories, function($filterBycategories){
        return $this->filterBy('categories', $filterBycategories);
    });

$pagination = $events->pagination();

?>

Thanks for your help.

Hi,

I think you need to move the pagination to after the filter, rather than before.

<?php 

$filterBycategories = get ('filter');

$events = $page
    ->children()
    ->listed()
    ->flip()
    ->when($filterBycategories, function($filterBycategories){
        return $this->filterBy('categories', $filterBycategories);
    })
    ->paginate(24)
;

$pagination = $events->pagination();

?>
1 Like

Oh, it was as simple as that! Thank you!