Search code examples
wordpressurlpaginationwordpress-themingpermalinks

Remove automatically added parameters to pagination URLs (custom permalinks)


Somehow when I changed the permalink of wordpress to a custom permalink the pagination links are not the same as before. Wordpress or the theme are adding query parameters to the url which I don't like. I tried already some of the solutions that I have found on Google, but without any success.

My pagination link is like that: https://myurl.com/custom-permalink/page/2?q=custom-permalink%2F

And I need it simply like that: https://myurl.com/custom-permalink/page/2


Solution

  • Thankfully I found the solution through this post: https://wordpress.stackexchange.com/a/78553

    I just needed to change the code a bit and now it works as expected and I get the pagination link like that: https://myurl.com/custom-permalink/page/2

    Just paste the code into your functions.php and it should work. Maybe you also have to go to the permalink settings in wordpress and click again on "save changes" after u added the code.

    The code:

    add_filter( 'get_pagenum_link', 'wpse_78546_pagenum_link' );
    
    function wpse_78546_pagenum_link( $link )
    {
        return preg_replace( '~/(\d+)/?~', '?page=\1', $link );
    }