WordPress Pagination with Custom WordPress Queries

I don’t remember the last time I made a WordPress theme that didn’t do all sorts of unique stuff. I’ve never been interested in making standard blog designs and always want to create something a bit different. This throws up some issues then – most often with pagination – and it’s only recently that I found a solution.

WordPress Pagination

WordPress Pagination

I had always assumed that the next_posts_link and previous_posts_link functions were for use in combination with ‘the loop’, but it seems that’s not the case. Upon closer inspection they just dissect the url and increment or decrement the number of pages. By default they use the properties inside the loop, but you can override them.

In fact it turns out the only value you need to pass is the maximum number of pages in the query and this can be accessed through the query itself.

Assuming you are using the WP_Query object (the proper way) then you can access the object property ‘max_num_pages’ to get this info.

So – the next time you do a custom WordPress Query, and you want some pagination to work with it, all you have to do is something like the following:

$custom_query = new WP_Query( $args );
// custom loop here
next_posts_link( __( '« Older Posts', 'mimbopro' ), $custom_query->max_num_pages );
previous_posts_link( __( 'Newer Posts »', 'mimbopro' ), $custom_query->max_num_pages );

How was it for you? Let me know on BlueSky or Mastodon

(Please) Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

Related Posts

01 Apr 2015

The State of WordPress Themes #wcldn

I recently spoke on a panel at WordCamp London 2015e. Lance – who used to be the Theme Team lead at WordPress.com – asked me if I wanted to speak on a panel with him at WordCamp London 2015. I’ve...
13 May 2010

6 Tips to Build Better WordPress Themes

If you want to make WordPress themes, for clients, to release for free or to sell, then there are a lot of factors you need to take into consideration. Below are some hints and tips that should help ease your...
27 May 2013

WordPress: 10 Years Young, What Does The Future Hold?

WordPress is now 10 years old. I started using wordpress 9 years ago – which means I joined the WordPress community early on. The reason I chose WordPress is simply because of the fabled 5 minute install process – I...
13 Oct 2016

Lessons Learned from 20 Theme Releases on WordPress.com

In 2007 I partnered with Darren Hoyt to release Mimbo Pro, one of the earliest premium WordPress themes. In 2012 Mimbo Pro was published on wordpress.com. Last week – on October 5th 2016 to be precise – my 20th theme...
20 May 2008

WordPress themes, and assumed responsibility

I’ve had a number of emails recently about an Islamic website that is using my free WordPress theme – Regulus. People have been emailing assuming that, because I designed the theme, I am a terrorist.I can’t actually comment on the...