Now I wonder if there is a way to do the same thing inside a collection of page, like tags, because if I click on a project after seeing it in a tag page, the next one is not the next in this filtered collection but the next child of global parent.
I wondered out of curiosity if there was a native simple way to remember the “real” next in collection that I miss or if I have to use session and complicated tricks to do it (in this case, I will not implement it)
You could use an URL parameter to pass the tag to the project page. Instead of using the standard page methods, you could create your own custom page methods https://getkirby.com/docs/developer-guide/objects/page, that use the information from the parameter to determine the next/prev pages (custom page methods will be available in the upcoming 2.3 release).
Edit: That would in fact work if you copy the code from the protected methods _prev() and _next() into your own page methods. Then you can pass a filtered collection to those methods like this.
Note that the above is just a rough outline to illustrate the idea using the native protected _prev() and _next() methods (by hacking the core to make them public for testing). Also, you would have to check if the referer exists etc.
The above methods are intended to replace the standard prev/next methods like they are used in the prevnext.php snippet of the starterkit. Instead of just fetching the next page, you would pass the collection to the method so that not only the sort order but also the filtering is taken into consideration when trying to get the next page.
The thing is that you have to pass the information about the collection down to the single page, so the $collectionvariable would be something like this
Ah yes, I understand now what is happening here. In this case you can load $collection (or another variable) with the list you want to navigate through with ‘next’ and ‘previous’. But the right order should already be in $collection.
Thanks for helping out and writing the custom page method. This is something that can be used in so many situations.