get_the_posts_pagination()
Gets pagination links for the next/previous set of posts. Used on archive pages (categories, tags).
For example, if 10 posts are displayed per page and there are a total of 50 posts in the category, then get_the_posts_pagination() will output links to 5 pages:

To display the result on the screen, use the_posts_pagination().
Hooks from the function
Returns
String. HTML code for pagination links. If there are no pagination pages, it will return an empty string.
Usage
get_the_posts_pagination( $args );
- $args(array)
Arguments responsible for outputting pagination. The following arguments are set by default:
$args = array( 'show_all' => false, // show all pages involved in pagination 'end_size' => 1, // number of pages at the ends 'mid_size' => 1, // number of pages around the current one 'prev_next' => true, // whether to display the side links "previous/next page". 'prev_text' => __('« Previous'), 'next_text' => __('Next »'), 'add_args' => false, // Array of arguments (query variables) to add to the links. 'add_fragment' => '', // Text to be added to all links. 'screen_reader_text' => __( 'Posts navigation' ), 'aria_label' => __( 'Posts' ), // aria-label="" for nav element. Since WP 5.3 'class' => 'pagination', // class="" for nav element. Since WP 5.5 );The parameter is passed to the function paginate_links(), so see there for the complete list of arguments.
By default: presets
Examples
#1 Usage Example
$pagination = get_the_posts_pagination( [ 'mid_size' => 2, 'prev_text' => __( 'Newer', 'textdomain' ), 'next_text' => __( 'Older', 'textdomain' ), ] );
$args param is passed to the paginate_links() function, so you can specify it's parameters too. Default values:
$args = array(
'base' => '%_%',
'format' => '?paged=%#%',
'total' => 1,
'current' => 0,
'show_all' => false,
'end_size' => 1,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => ''
);
$pagination = get_the_posts_pagination( $args ); #2 Output pagination links
Suppose we need to display pagination links to the sets of posts (/page/1, /page/2) on the archives page, use the following code:
<?php echo get_the_posts_pagination(); ?>
It displays:
<nav class="navigation pagination" role="navigation"> <h2 class="screen-reader-text">Posts navigation</h2> <div class="nav-links"><span class="page-numbers current"><span class="meta-nav screen-reader-text">Page </span>1</span> <a class="page-numbers" href="http://example.com/page/2/"><span class="meta-nav screen-reader-text">Page </span>2</a> <span class="page-numbers dots">…</span> <a class="page-numbers" href="http://example.com/page/86/"><span class="meta-nav screen-reader-text">Page </span>86</a> <a class="page-numbers" href="http://example.com/page/87/"><span class="meta-nav screen-reader-text">Page </span>87</a> <a class="next page-numbers" href="http://example.com/page/2/">Next page</a> </div> </nav>
Notes
- Global. WP_Query. $wp_query WordPress Query object.
Changelog
| Since 4.1.0 | Introduced. |
| Since 5.3.0 | Added the aria_label parameter. |
| Since 5.5.0 | Added the class parameter. |