WordPress Snippets at WPcustoms

Limit CPTs per page on homepage

What would we do without the pre_get_posts filter? Another handy WP snippet to customize the posts per page on your homepage for custom post types. In this example the query will only show 3 posts on your homepage from the “movies” and “books” post type.

 * Snippet Name: Limit CPTs per page on homepage
 * Snippet URL: https://wpcustoms.net/snippets/limit-cpts-per-page-homepage/
  function wpc_limit_main_query( $query ) {
  if ( $query->is_main_query() && is_home() ) { 
// only limit the posts_per_page on your homepage

    $query->query_vars['posts_per_page'] = 3; //displays 3 items 
    $query->query_vars['post_type'] = array('movies', 'books'); //add your CPTs here

    return $query;

add_action('pre_get_posts', 'wpc_limit_main_query', 1);