Last page in pagination does not match max_num_pages - the overall post count is less according to pagination
I'm using paginate_links() to display my posts pagination like this:
$paged = get_query_var('paged');
$current_page = max(1, $paged);
$custom_query = array(
'post_status' = 'publish',
'posts_per_page' = 4,
'paged' = $current_page
);
$query = new WP_Query($custom_query);
if($query-have_posts()) {
// The pagination comes after displaying the posts:
echo paginate_links(array(
'base' = get_pagenum_link(1) . '%_%',
'format' = 'page/%#%',
'current' = $paged,
'total' = $query-max_num_pages,
'prev_text' = 'Prev',
'next_text' = 'Next'
));
}
To be specific with numbers, there are 571 posts on the website. I am displaying 4 posts per page. Which would make the max_num_pages
143 = 571 / 4.
If I don't specify the total
parameter in paginate_links()
, the pagination by default will say that there are 84
pages.
So WordPress thinks there are 335 posts instead of 571. That's why it's showing 84 pages by default = 335 / 4.
I'm able to get the pagination working correctly by going into Settings Reading and changing Blog pages show at most [1] posts instead of having it set to 4. But this is just a hack and the page title is off. For example, if I'm viewing the last page (143), it will say Page 143 of 335 instead of Page 143 of 143.
Any help would be greatly appreciated.
Topic paginate-links pagination Wordpress
Category Web