How to test pagination? [closed]

The homepage and all archive pages (date, category, tag, taxonomy and author pages ) uses the same default pagination functions for pagination. What this means is, the_posts_pagination works on all archive pages and the home page. The only differences on these pages is the content which is determined by the main query.

As far as pagination goes, it is calculated the same on all of these pages, no difference here, and this also goes for the main query and all custom instances of WP_Query. WP_Query use the following parameters to calculate pagination

  • posts_per_page – This value is constant across the complete site, including custom queries. WP_Query uses the amount of posts per page set in the backend under the reading settings page to calculate how many posts to show per page when no custom value is set (like for custom queries). This default option can be returned with get_option( 'posts_per_page' ). If you need a custom value for the main query for a specific archive page or the home page, you should use pre_get_posts to set this custom value

  • offset – To page a query, WP_Query uses offset to skip a set of posts in order to show the correct posts according to page number you are on. The current page number can be returned with get_query_var( 'paged' ). Page 1 will be 0 and there after the value of get_query_var( 'paged )will be equal to the page number. Just as example, ifposts_per_pageis set to10, and you are viewing page 3,WP_Queryhas used an offset of20` to skip the first 20 posts to correctly show posts 21 – 30 on page 3

  • $found_posts – This property holds the amound of posts found by WP_Query which matched the query. By default, WP_Query does not stop searching the db once it found the amount of posts queried. It will continue throughout the db and count all the posts that matches the current query. So, if you have 10 posts per page, WP_Query will return only 10 posts in the query, but it does not bail out once that 10 posts is filled. It continue to count posts that matches the query, so if there are 100 posts matching the current query, the value of $found_posts will be 100. You can check this by echoing the result of $wp_query->found_posts

All of the above goes into the calculation of the $max_num_pages property of the WP_Query object. This is the important value used by pagination functions like the_posts_pagination to display the correct amount of pages and when to display pagination links. If the value is 1, the paging links will not show, these links only show if the value is bigger than 1. By default, the main query object value is passed to these pagination functions. You can check the value of this property for the main query object by echoing the value of $wp_query->max_num_pages.

You can do the following on your archive page (or any page) to display how many pages there are, what the setting of posts per page is and on which page we are

$ppp = get_option( 'posts_per_page' );
$page_number = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1
$total_pages = $wp_query->max_num_pages;
$show_pagination = get_query_var( 'paged' ) ? 'show' : 'not show';

echo 'My posts_per_page setting for this page is ' . $ppp . ' with an amount of ' . $total_pages . ' pages of posts. According to this, my pagination function should ' . $show_pagination . '. Do not forget, I am currently viewing page ' . $page_number . '.';