Passing custom args in paginate_links

All we need is an easy explanation of the problem, so here it is.

For example i’ve domain.com/about?project=1 but i’ve permalinks enabled and i’m using thins to generate the pagination paginate_links. The proble is that the links generated are like this domain.com/about?project=1/page/1 and it breaks everything because the project is taken as 1/page/1

There is a way to get domain.com/about/project/1/page/1 in the paginations links? I’m submitting that from a form using get to display the project.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

To add arguments to the links in paginate posts, use the 'add_args' argument in the function. You have to pass the arguments as an associative array. So, to add project=1 to the end of all your links, you would do this:

global $wp_query;
paginate_links(array(
  'total' => $wp_query->max_num_pages,
  'current' => (get_query_var('paged') ? get_query_var('paged') : 1),
  'base' => 'http://domain.com/about/%_%',
  'format' => 'page/%#%',
  'add_args' => array( 'project' => 1 /* or whatever the project number is*/ ),
));

Hope that helped!

EDIT

To get domain.com/about/projects/1/page/1, you can add a custom permastruct. I’m going to assume about is a page.

function wpse21802_init(){
  add_rewrite_rule( '([^/]+)/projects/([^/])/?$', 'index.php?pagename=$matches[1]&project=$matches[2]', 'top' );
  add_rewrite_rule( '([^/]+)/projects/([^/])/page/(/d+)/?$', 'index.php?pagename=$matches[1]&project=$matches[2]&paged=$matches[3]', 'top' );
}
add_action( 'init', 'wpse21802_init' );

After adding that code, flush the rewrite rules by going to Settings -> Permalinks.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply