Calculating postOffset for Custom Pagination

[ad_1]

I have a custom page with pagination enabled for my website, [Market Cap Stats]). The code below works fine when postsPerPage=10, but as soon as I increase it to 11 or 100, I run into the following issues:

1. My homepage displays n-1 items (ie 99 instead of 100)
2. When I click to go to page 2, the page skips items 100-199 and starts at 200

Items 200-infinity load properly. It’s only the second page I am having issues with.

Here is my code for generating the page:

$args_page = array(
‘post_type’ => ‘page’,
‘offset’ => $postOffset,
‘posts_per_page’ => $postsPerPage,
‘meta_key’ => ‘market_cap_rank’,
‘orderby’ => ‘meta_value_num’,
‘order’=>’ASC’
);

Here is my code for pagination:

<?php endwhile;
echo paginate_links( array(
‘base’ => str_replace( 999999999, ‘%#%’, esc_url( get_pagenum_link( 999999999 ) ) ),
‘total’ => $postslist->max_num_pages,
‘current’ => max( 1, get_query_var( ‘paged’ ) ),
‘format’ => ‘?paged=%#%’,
‘show_all’ => false,
‘type’ => ‘plain’,
‘end_size’ => 2,
‘mid_size’ => 1,
‘prev_next’ => true,
‘prev_text’ => sprintf( __( ‘<‘, ‘text-domain’ ) ),
‘next_text’ => sprintf( __( ‘>’, ‘text-domain’ ) ),
‘add_args’ => false,
‘add_fragment’ => ”,
) );

Any idea what I am doing wrong here? My guess is the issue is with pageOffset.

[ad_2]
1 Comment
  1. Have you tried without the `offset` param? I don’t use that in my CPT pagination and it works fine.

    I also see you’re not passing the `paged` param.

    Here’s mine:

    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;

    $args = array(

    ‘post_type’ => ‘members’,
    ‘posts_per_page’ => 10,
    ‘paged’ => $paged,
    ‘orderby’ => ‘menu_order’,
    ‘order’ => ‘ASC’

    );

    global $allBoards; // the query results

    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
    ‘base’ => str_replace( $big, ‘%#%’, esc_url( get_pagenum_link( $big ) ) ),
    ‘format’ => ‘?paged=%#%’,
    ‘current’ => max( 1, get_query_var(‘paged’) ),
    ‘total’ => $allBoards->max_num_pages,
    ‘prev_text’ => ‘<span aria-label=”Previous Page” class=”fa fa-chevron-left nav”></span>’,
    ‘next_text’ => ‘<span aria-label=”Next Page” class=”fa fa-chevron-right nav”></span>’
    ) );

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer