Changing the Number of Posts Displayed on a Page

In WordPress, there is a global setting that determines how many posts to display on a page — posts_per_page. The best way is to change this parameter before the main query, for the sake of resource savings, so that we don’t make repeated queries. Thus, we can use the pre_get_posts action hook to change the number of posts displayed on a page.

This example shows how to override the posts_per_page parameter for the archives page of a custom post type movie:

add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );
function hwl_home_pagesize( $query ) {

	// Exit if this is the admin panel or not the main query.
	if( is_admin() || ! $query->is_main_query() )
		return;

	if( is_home() ){
		// Display only 1 post on the home page
		$query->set( 'posts_per_page', 1 );
	}

	// Display 50 posts if this is a post type archive of 'movie'
	if( $query->is_post_type_archive('movie') ){
		$query->set( 'posts_per_page', 50 );
	}
}

Note embeded into: pre_get_posts