WordPress Pagination Custom Rewrite Rule for Pages

If the data is coming from a third-party, then putting rules into the Permalinks section of the WP admin panel won’t work.

You’ll need to write some code to register your own URL parameter(s) for the pagination, in order to achieve clean URLs.

First, you need to register the parameter, I suggest not calling it page as this may conflict with WP’s native ones.

function custom_query_vars_filter($vars) {
  $vars[] = 'pg';
  return $vars;
}
add_filter( 'query_vars', 'custom_query_vars_filter' );

After adding your custom query vars, you need to flush the permalinks for WP to pick up on your new custom query vars – to do this, simply resave the permalink structure in the admin panel settings.

Then, wherever you are outputting a link to a URL that includes your custom paging URL parameter, do this to create the link:

add_query_arg(array('pg' => 2), '/pagename');

And then on the page, to get the page number to use in the query to the API, do this:

get_query_var('pg');

Also, you need to set permalink structure to post name (in the admin panel settings) if you haven’t already.