How to get a variable number of posts per post type on the main loop?

If you are still looking for an alternative that may be faster this may help you:

<?php
function customBlogFeed() {

// The Query 
$the_query = new WP_Query( array ( 'post_type' => array( 'post', 'page', 'movie', 'book'), 'posts_per_page' => '6' ) );
//Your post_type array is a list of random post_types. You can add whatever you'd like to match your system.

// The Loop 
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>


  <?php the_title(); ?>
      <?php the_content(); ?>

<?php endwhile;

// Reset Post Data
wp_reset_postdata();
}
?>

Then to get it’s output put <?php customBlogFeed(); ?> wherever you’d like this to output.

If you really want to get fancy you can hook into the post_limits Filter and limit how many posts per post type are displayed. I hope this helps you on your quest.

PS – Look into WP_Query, it will really help you out.

After some research you might actually want to look into post_clauses to get those SQL characterizations done with WP3.1+ Syntax

Leave a Comment