WordPress Pagination in Custom Loop

Post by 
John Crenshaw
Published 
October 16, 2014

If you use custom loops in WordPress you may run into pagination issues. Specifically, no matter what page you're on, only the first page's worth of posts are displayed. Here's a really quick solution to that problem using the query_posts() function.

I wrote an article a while back about using a static homepage for your WordPress blog. In that article I mentioned using a custom loop on this blog in the blog and tutorials sections. One problem you may run into when using a custom loop is that pagination doesn't work like you'd expect it to.In particular, you may notice that only the first page of posts are displayed no matter what page of posts you're trying to view. I often use WordPress as a general purpose CMS in client projects and so I use a lot of custom loops and run into this issue a lot. Here's the solution:

Update query_posts with the proper query vars

The solution to this problem is pretty simple. You need to tell WordPress to use the "paged" query variable in your custom loop, otherwise it's ignored. If you're using the query_posts function for your custom loop, the easiest way to do this is like so:

1
2
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('paged=' . $paged . '&cat=76'); // show posts in category 76 with pagination enabled

In this example, we first test to see if the "paged" query variable is set using the get_query_var function, which will return false if it's not set. If it is set, we'll set $paged equal to the value of that query variable and if not, we'll set it equal to 1.

Then we initialize our custom loop using the query_posts function by setting "paged" equal to our $paged variable so that WordPress grabs the current page from the URL and displays the appropriate list of posts for that page.

I'm also looping through the posts in category #76 in this example, which is only there for illustrative purposes. That's all there is to enabling pagination in custom WordPress loops.

Never miss
an update

We never share your info. View our Privacy Policy
Almost there. Follow these 2 steps to complete your subscription:

1. Click the link in the confirmation email we just sent you

2. Add [email protected] to your address book

Thanks!
Oops! Something went wrong while submitting the form.
THere's More

Post you might also like

All Posts
Analytics
Oct
17
//
2020

Causes & Solutions to Website Traffic Drops After a Redesign

New websites are supposed to outperform the old one, but that doesn't always happen.
Strategy
Oct
15
//
2020

This Famous Ad Exec Talks About a Big Brand Who's Existence Just Doesn't Make Sense

The irrational product that kicked off a $4 billion brand
Strategy
Oct
12
//
2020

How to Gain Market Share

Find out what it takes to grow.
Strategy
Jul
15
//
2020

Marketing Idea 0001: Target the Larger Audience

Sometimes customers that want to rent your product are actually better candidates to buy it.
Paid Media
Jun
12
//
2020

PPC vs CPC: What's the Difference

They aren't the same thing, but they're pretty closely related