Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Custom Post Pagination in WordPress

DZone's Guide to

Custom Post Pagination in WordPress

Learn how to paginate custom posts in WordPress in just a few simple steps. Plus, learn the pitfalls that others are always dealing with and how to handle them.

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

If you’re dealing with lots of custom posts in WordPress and if you’re a theme developer, this is probably a well known issue to you. Lots of people are discussing this pagination thing in different forums. However, it’s really not that tough to paginate custom posts. Let’s find out how we can do it easily.

Query CPTs

This is the first thing you are going to need, is to write a custom query. Because when you have a bunch of posts in your collection, you will start thinking about the pagination. So it’s time to find some posts. The following code will find all the posts of post type book and display their titles

$books = new WP_Query( array(
"posts_per_page" => - 1,
"post_type" => "book"
) );

while ($books->have_posts()){
$books->the_post();
the_title();
echo "<br/>";
}

Now you’re probably going to use the good old next_posts_link() to display the next page. Well, that will not work because at this point next_posts_link() doesn’t know how many pages are there. So to make it work, you’ll have to pass the number of total pages found by your query. How is this total number of pages are calculated by the way?

$total_pages = ceil( $books->found_posts / get_option('posts_per_page') );

Interestingly, you don’t need to calculate the total number of pages like this. A WP_query object already did it for you and made it available via max_num_pages property.

$total_pages = $books->max_num_pages;

Now we need that value to show the link to our next page. Just pass it to next_posts_link() like this

next_posts_link('Prevous Posts',$books->max_num_pages)

Now it wil show a valid link that can take you to the previous posts page. If your url is http://yoursite.com/books/ then, the link of the previous posts will be of this format http://yoursite.com/books/page/2. So you’re going to need this page number in your PHP script to fetch all the posts (or CPTs) from that page. In your theme, you can get this page number using a query var called paged. The following code will correctly fetch the current page number

$current_page = get_query_var('paged') ? get_query_var('paged'):1;

To fetch all the posts from this page, you just need to rewrite your WP_Query like this

$books = new WP_Query( array(
"posts_per_page" => - 1,
"post_type" => "book",
"paged" => $current_page
) );

In case you’d like to see a traditional pagination using page numbers, you will have to use the following code

paginate_links( array(
"current" => $current_page,
"total" => $books->max_num_pages
) );

And you're done :)

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:
wordpress ,php

Published at DZone with permission of Hasin Hayder. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}