WordPress VIP and other development guides I've seen recommend avoiding the use of post__not_in for performance reasons when using WordPress at scale. Their suggestion is to filter out the posts in php:
$posts_to_exclude = [ 67, 68, 69 ];
$query = new WP_Query( [
'post_type' => 'post',
'posts_per_page' => 5 + count( $posts_to_exclude ),
'paged' => get_query_var( 'paged' ),
] );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
if ( in_array( get_the_ID(), $posts_to_exclude, true ) ) {
continue;
}
the_title();
endwhile;
endif;
My question is how do you show the correct number of posts per page when filtering out the posts via php? The example above adds the count of the excluded posts to the posts_per_page variable. However this would result in there being between 5 – 8 posts per page depending on how many posts are excluded for a particular page. Is there a workable solution for making the number of posts per page consistent or is this optimisation only really meant for posts that aren't going to be paginated?