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 );

Let me know what you think on Mastodon, or BlueSky (or Twitter X if you must).

WordPress News

The latest WordPress updates from the WPBriefs Podcast.

Related Posts

11 Apr 2013

WordPress Query: Exclude Posts With No Featured Image

So – I make WordPress themes – lots of them 🙂Something that’s bugged me for a while is that I can’t easily use the WP_Query to select posts with featured images. This isn’t functionality that’s needed that often – but...
27 Oct 2013

WordPress Numeric Pagination

I’ve made quite a few WordPress themes in my time, and so I thought I would write a few blog posts sharing some of the code snippets that I find myself using repeatedly.First off is some numeric pagination using native...
30 Mar 2010

10 WordPress query_posts tips you probably don’t know

I have written a really brief query_post tutorial before, and it did quite well, but both WordPress and my own skills, have advanced considerably since then. So I thought it would be interesting to revisit the query_posts command and see...
28 May 2010

WordPress caching, Part 2

As I mentioned in WordPress caching part 1, WordPress has built-in caching that can be hooked into by plugins such as W3 Total Cache and Batcache (developed by Andy Skelton who is employed by Automattic).In this article I am going...
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...
22 Apr 2010

How to Build the Perfect WordPress Sitemap

I was recently asked how I built the sitemap on Binary Moon so I thought I would share the code. The sitemap layout is actually one of the many custom page templates available in my WordPress theme, Elemental – however...