Different Number of Posts on the First Blog Page
Shows a different number of posts on the first page of the blog than on the following pages, for example 3 featured posts up front and 10 per page after that, with pagination still correct.
Opens WPIDE on
Code
// Number of posts on the first page of the blog.
$wpide_first_page_posts = 3;
add_action( 'pre_get_posts', function ( $query ) use ( $wpide_first_page_posts ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_home() ) {
return;
}
$per_page = (int) get_option( 'posts_per_page' );
$paged = max( 1, (int) $query->get( 'paged' ) );
if ( 1 === $paged ) {
$query->set( 'posts_per_page', $wpide_first_page_posts );
} else {
$query->set( 'posts_per_page', $per_page );
$query->set( 'offset', $wpide_first_page_posts + ( $paged - 2 ) * $per_page );
}
} );
add_filter( 'found_posts', function ( $found, $query ) use ( $wpide_first_page_posts ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_home() ) {
return $found;
}
$per_page = max( 1, (int) get_option( 'posts_per_page' ) );
$pages = 1 + (int) ceil( max( 0, $found - $wpide_first_page_posts ) / $per_page );
if ( max( 1, (int) $query->get( 'paged' ) ) === 1 ) {
return $pages * $wpide_first_page_posts;
}
return $found - $wpide_first_page_posts + $per_page;
}, 10, 2 );
Notes
Later pages use the "Blog pages show at most" setting. Sticky posts are added on top of the first-page count by WordPress.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.