Hi, I’ve solved a problem similar to yours.
In my case the problem was cause by the fact that the page is attached to the current url.
So, if you are on the single page example.com/posts/post1, the function returns a link like example.com/posts/post1/page/2/
i.e.: there is a “post1” that should not be there. I’ve resolved in a brutal way playing with strings functions…