WordPress Pagination – Too many pages for number of posts

I’ve added pagination for a custom post type within my WordPress theme. It works great, apart from it shows too many pages in the pagination menu for the number of posts: http://www.electrickiwi.co.uk/testimonials/

There should currently be 7 pages, but it’s showing 12. The code below is what I am using to display the pagination. There is nothing in the functions.php file relating to this.

<?php
/* ------------------------------------------------------------------*/
/* PAGINATION */
/* ------------------------------------------------------------------*/

//paste this where the pagination must appear

global $wp_query;
$total = $wp_query->max_num_pages;
// only bother with the rest if we have more than 1 page!
if ($total > 1) {
    // get the current page
    if (!$current_page = get_query_var('paged')) {
        $current_page = 1;
    }
    // structure of "format" depends on whether we're using pretty permalinks
    if (get_option('permalink_structure')) {
        $format = 'page/%#%/';
    }
    else {
        $format = 'page/%#%/';
    }
    echo paginate_links(array(
        'base' => get_pagenum_link(1) . '%_%',
        'format' => $format,
        'current' => $current_page,
        'total' => $total,
        'mid_size' => 4,
        'type' => 'list'
    ));
}
?>

Related posts

Leave a Reply

3 comments

  1. I was having this exact same issue, but was able to solve it by using ‘total’ => $my_query->max_num_pages, like this:

    $args[ 'post_type' ] = array('resources', 'case_study');
    $args['post_status'] = 'publish';
    
    $my_query = new WP_Query( $args );
    echo the_posts_pagination( array(
        'mid_size' => 3,
        'total' => $my_query->max_num_pages,
    ) );