Limit number of pages in pagination

Before I start, do not use get_posts for paginated queries. get_posts legally breaks pagination, and does not return the query object. If you need to paginate queries, use WP_Query

As for your issue, I really don’t think limiting the total overall amount of posts to only 100 is possible when you involve pagination. The only scenarios I can think of is:

  • Remove the page links on page 10, but again you can manually enter page 11 and see page 11 posts

  • Run a custom function when a new post is published to change the post status of 101st post and sequencial posts to something else, like draft. By default, WP_Query only queries published posts for all users and extra private posts for logged in users. So if you change post status, these posts will be excluded

  • Another, less intrusive method will be to run two queries. The first one, which will be very basic and lean, will return the post ID’s only of the first 100 posts. This ID’s will be passed to the second query which will get 10 posts per page and have only 10 pages because we just pass 100 ID’s to it. The advantage here is, if you manually enter page 11, you’ll get a 404 page.

EXAMPLE OF POINT 3

As I have said, point 3 is the best option here. We will use get_posts for the first query as it cuts out pagination, which speeds up your query, and we will only get the post ID’s which makes the complete query 99.9% faster than normal. Overall, the impact on the page is nearly zero. For our second query we will use WP_Query and the post__in parameter to get our paged list of posts

(This code is untested and requires PHP 5.4+)

$args = [
    'posts_per_page' => 100,
    'fields' => 'ids',
    // Add additional args here
];
$post_ids = get_posts( $args );

if ( $post_ids ) {
    $args_2 = [
        'paged' => $paged,
        'post__in' => $post_ids,
        'posts_per_page' => 10,
    ];
    $q = new WP_Query( $args_2 );
    while ( $q->have_posts() ) {
    $q->the_post();
        // YOUR LOOP
    }
    next_posts_link( 'Next Posts', $q->max_num_pages );
    previous_posts_link( 'Previous Posts' ); 
    wp_reset_postdata();
}

Leave a Comment