Hello, I am trying to both do filtering and sorting of articles at the same time. I did everything like it says in this Cookbook entry and managed to get filtering working to my liking. Now I want to sort these entry via links on the page the same way i am filtering. I guess the best way to go would to append the sort url parameter?
I already got it sorted by date without it breaking.
I would like it to “cycle” through ascending and descending. So you click the link, adding the parameter to the url (including e.g. ascending) and on the next click it would change that parameter to descending.
Hmmm i cant get it to work. I don’t understand what function $notes = $works serves here. Right now passing the params to the URL works fine, but neither sorting nor filtering is applied to $works.
Also i have trouble figuring out when the syntax would allow spaces and when it would not. For example here: fn ($filterBy)
Awesome! This works! One last thing: Is it possible to have “default” sorting? So the initial is not unsorted but rather already being sorted by e.g. date? Thank you so much, this forum is really the best!
Well, yes, of course you can filter the original collection before applying the other filters conditionally.
$works = $page
->children()
->listed()
->sortBy('whatever')
->when($filterBy, fn($filterBy) => $this->filterBy('category', $filterBy))
->when($sortBy, function($sortBy) {
// depending what you want to sort by use conditions here
return $this->sortBy($sortBy);
});