0

I am using the built in paginate_links function and setting formatparameter to the default value:
'?paged=%#%' . For some reason, it keeps redirecting to a different formatted url. For example, when I click the paginated link to page two it should be www.domain.com/subpage/?paged=2, but instead it's formatted as www.domain.com/subpage/page/2 . Not sure how to stop this redirect? Any ideas?

Here's the function with the parameters I'm using:

function custom_page_navi( $totalpages, $page, $end_size, $mid_size )
{
    $bignum = 999999999;

    if ( $totalpages <= 1 || $page > $totalpages ) return;

        //NOTE: https://codex.wordpress.org/Function_Reference/paginate_links
    return paginate_links( array(
        'format'        => '?paged=%#%',
        'current'       => max( 1, $page ),
        'total'         => $totalpages,
        'prev_next'     => false,
        'type'          => 'list',
        'show_all'      => false,
        'end_size'      => $end_size,
        'mid_size'      => $mid_size
    ) );
}

1 Answer 1

1

This is a problem with the paginate_links function and having your default permalink structure set to postname/custom but wanting the pagination to use default link structure.

If you look at the code - https://core.trac.wordpress.org/browser/tags/4.8/src/wp-includes/general-template.php#L0

You can see the following at the top of the paginate_links function

// Setting up default values based on the current URL.
$pagenum_link = html_entity_decode( get_pagenum_link() );
$url_parts    = explode( '?', $pagenum_link );

Then lower you will find the following:

// Merge additional query vars found in the original URL into 'add_args' array.
if ( isset( $url_parts[1] ) ) {
    Bunch of code here to set the format...
}

So the problem is when you use the postname permalink structure the $url_parts variable will only return 1 array value and the custom format checks only run if there are 2 return values in the array. And the code inside this if statement is what parses the format argument.

Therefore your format argument is completely ignored :(

If you go to Settings > Permalinks and you set your structure to "Plain" you should see the pagination links working as you would like.

You could try filtering the 'get_pagenum_link' function to return a "plain" link since the main issue is that this function ignores the format defined in paginate_links and instead uses the $wp_rewrite->using_permalinks() check - https://developer.wordpress.org/reference/functions/get_pagenum_link/

1
  • Thanks! I would preferably like to keep that URL format (www.domain.com/subpage/page/2) but I think it's because I set one of my custom taxonomies to have a custom rewrite of /subpage/. Pagination only works when the url is formatted as plain (www.domain.com/subpage/?paged=2) Do you think I should use an add_rewrite() ?
    – bhood
    Commented Jul 24, 2017 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.