Support » Developing with WordPress » Using the_posts_pagination with offset adds extra empty page

  • I’m working on a quite complex query, it has a different post count on the first page and on paginated pages, to achieve that I’m using offset and this seem to mess up the_posts_pagination.

    Here’s the code:


    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
     //post content here...
    <?php endwhile; endif; ?>
        the_posts_pagination( array(
            'mid_size'  => 2,
            'prev_text' => __( 'Prev', 'textdomain' ),
            'next_text' => __( 'Next', 'textdomain' ),


    function my_offset( $query ) {
        $ppp = get_option( 'posts_per_page' );
        $first_page_ppp = 3;
        $paged = $query->query_vars[ 'paged' ];
        if( $query->is_home() && $query->is_main_query() ) {
            if( !is_paged() ) {
                $query->set( 'posts_per_page', $first_page_ppp );
            } else {
                $paged_offset = $first_page_ppp + ( ($paged - 2) * $ppp );
                $query->set( 'offset', $paged_offset );
    add_action( 'pre_get_posts', 'my_offset' );
    function my_offset_pagination( $found_posts, $query ) {
        $ppp = get_option( 'posts_per_page' );
        $first_page_ppp = 3;
        if( $query->is_home() && $query->is_main_query() ) {
            if( !is_paged() ) {
                return( $found_posts );
            } else {
                return( $found_posts - ($first_page_ppp - $ppp) );
        return $found_posts;
    add_filter( 'found_posts', 'my_offset_pagination', 10, 2 );

    The post per page is set to 5 in the reading settings, but the issue is there no matter what it’s set to.

    Now using this code the the_posts_pagination will show at least one extra empty page in the numbered pagination links. However if I go to the second page then the numbered pagination will display the correct number of max pages.

    Any help is much appreciated

Viewing 1 replies (of 1 total)
  • Your filter for the found posts for your example number is subtracting a negative number, which of course adds. But even if you fix that, the max number is set using
    $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
    which is taking the posts per page number from the query.
    You probably should take the advice of the comment and set max_num_pages yourself:

      * Filtering functions that require pagination information are encouraged to set
      * the <code>found_posts</code> and <code>max_num_pages</code> properties of the WP_Query object,
      * passed to the filter by reference. If WP_Query does not perform a database
      * query, it will not have enough information to generate these values itself.

    Also, is your is_paged() really working as you think, since it’s before the query?
    This is part of that code:
    Conditional query tags do not work before the query is run. Before then, they always return false.

    • This reply was modified 1 year, 8 months ago by Joy. Reason: forgot to mention
Viewing 1 replies (of 1 total)
  • The topic ‘Using the_posts_pagination with offset adds extra empty page’ is closed to new replies.