I consider the right way could be filtering of total number of found posts like this.
function my_custom_found_posts_limiter( $found_posts, $wp_query ) {
$maximum_of_post_items = 100; // place your desired value here or read if from option\setting.
if ( ! is_admin() && $wp_query->is_main_query() && $wp_query->is_post_type_archive( 'blog_posts' ) ) {
if ( $found_posts > $maximum_of_post_items ) {
return $maximum_of_post_items; // we return maximum amount, so pagination will be aware of this number.
}
}
return $found_posts;
}
add_filter( 'found_posts', 'my_custom_found_posts_limiter', 10, 2 );
See source code here https://core.trac.wordpress.org/browser/tags/5.3/src/wp-includes/class-wp-query.php#L3234
and lines after this filter is applied to have better understanding of how it will work.
NB: I’ve used is_main_query()
conditional and is_post_type_archive
meaning it will be used for main Post archive loop or CPT archive page loop, but you can adjust the way you want.
UPD: added !is_admin()
– check so it will not fire in wp-admin.