Yesterday, I talk about using WP_query or query_posts or get_posts and today I am going to explain the approach I use to have pagination using get_posts(). The other two methods take care of the pagination by them self by just passing the paged parameter along with the function call. But get_posts() is more of a raw function which I used to create paginated data on the basis of already existing pagination.
Here for the sake of explanation of approach, I am making the content paginated where content is already paginated. We will make our extra content work in order to the global $paged variable.
<?php // Posts Per Page option $ppp = get_option('posts_per_page'); if (!is_paged()) { $custom_offset = 0; } else { $custom_offset = $ppp*($paged-1); } // Lets suppose we are querying for posts of a certain author in a particular category $args = array( 'numberposts' => $ppp, 'offset' => $custom_offset, 'category' => 7, // Category ID of category 'Articles' 'author' => $author_id ); $posts_data = get_posts( $args ); if ( count( $posts_data ) > 0 ) { echo '<ul>'; foreach ( $posts_data as $post ) { echo '<li><a href="'.get_permalink( $post->ID ).'">'.$post->post_title.'</a></li>'; } echo '</ul>'; } else { echo '<p>No articles by this user</p>'; } ?>
This way the code make use of the global $paged variable to see which page it is on, and set the value of offset accordingly making the fetched content using get_posts itself paginated. On Page 1, the offset will be zero, it will fetch the posts limited by the posts per page value and on Page 2, the offset will be calculated to leave the posts already shown on previous page and query further posts limited by the posts per page value.
Got questions? Comment section is down below.
Comments
4 responses to “Pagination approach using get_posts() in WordPress”
Nice tutorial! It helped me in one of my wordpress website development projects.
Ash, I came here looking for a solution to display a different number of post/excerpts on archive pages, like category pages? For example, on Page 1 of a category archive display 5 posts, while on each of the successive category archive pages (Page 2, Page 3, etc.) display 10 posts.
Can the code appropriate for this? If not, do you know what modifications might be required?
Wahoooooo!!
What a momentous thing!! Was going round in circles trying to get WP_query to do what I wanted and failing. I think sticky posts may be the reason I was having problems. By using get_posts everything is back under control!! Thanks Ash.
Much less messy than the WP_query stuff I was playing about with. But probably only appropriate where only one loop on page?
The simple code above even works with the next_posts_link() and previous_posts_link() functions, as long as you want to display all the posts that the default wp_query contains.
But the code could also be customised to work with any get_posts query by working out how many posts the query will return, WITHOUT either the Offset or Paged parameters set & numberposts set to -1 to return all posts. Then based on this, you can calculate how many pages to expect. In the example above:
$args_no_offset = array (
‘numberposts’ => -1,
‘category’ => 7,
‘author’ => $author_id,
);
// imagine $ppp = 10
$total_posts = count($args_no_offset); // e.g. 33
$numpages = ceil($total_posts / $ppp); // e.g. ceil(33/10) = ceil(3.3) = 4
// so total pages expected is 4
// use page link functions with this number
next_posts_link(‘More »’,$numpages)
// etc.
Nice! I have never used adjacent post links function but good thing you found a way. And about using multiple loops, sure you can.