WordPress custom per page on paged archive vs first page

I ran into a situation where we wanted to have 10 articles featured on the main Blog page, but then as they paginated through to show 20 per page. The below code uses the “pre_get_posts” filter and some conditionals to make that happen:

add_action( 'pre_get_posts',  'custom_per_page_on_paged'  );
function custom_per_page_on_paged( $query ) {
	if ( $query->is_main_query() && !is_admin() && $query->is_paged() ) {
		$per_page_first = 10;
		$per_page_archives = 20;
		$query->set( 'posts_per_page', $per_page_archives );
		$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
		$query->set( 'offset', ( ( $paged - 2 ) * $per_page_archives ) + $per_page_first );
	}
}
About the Author
Derek Ashauer is the sole developer at WP Sunshine. He also does client work through his agency AshWebStudio where he has been working with WordPress since 2005.