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.

x_posts_link

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

Old books image originally from Shutterstock.

Share This...

About The Author

Ben Gillbanks

Web Designer, Video Gamer, Blogger, and part time Entrepreneur. Read More

One thought on “WordPress Pagination with Custom WordPress Queries

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Websites linking here

  1. Favicon for http://www.google.com/s2/favicons?domain=bengillbanks.co.uk WordPress Pagination with Custom WordPress Queries

My Projects

TimThumb - Image Resize Script TimThumb

Image Resize Thumbnail Script

WPVote - WordPress Social Voting WPVote.com

WordPress Social Voting Site

About me

About BenMy name is Ben Gillbanks. I'm a lover of Video Games, WordPress, Web Development and everything in between.

I have been working on the internet since 1998, and working with computers even longer. I am a hardcore Nintendo fanboy and have owned most of their consoles at one stage or another.

Read more about me on my about page.

My Tweets › Binary Moon

Random Link-outs

Keep Updated

Subscribe to RSS

Stay Updated

Binary Moon

WordPress and Web Development › home of Ben Gillbanks