Title: Always three posts from multiple custom post types
Last modified: August 19, 2016

---

# Always three posts from multiple custom post types

 *  Resolved [thiesenmurray](https://wordpress.org/support/users/thiesenmurray/)
 * (@thiesenmurray)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/always-three-posts-from-multiple-custom-post-types/)
 * I’m having a problem with a custom query loop. I want on the homepage always 
   a number of three posts. Some of that posts are events so sometimes there are
   three but there are also times that there are zero or two posts. I always want
   to fill in the left over spaces with other posts from other custom post types.
   The events are the most important so I want to say in my loop: count the number
   of events and if there’re less then three, show another custom post type to fill
   up the three.
 * Thanks in advance

Viewing 1 replies (of 1 total)

 *  Thread Starter [thiesenmurray](https://wordpress.org/support/users/thiesenmurray/)
 * (@thiesenmurray)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/always-three-posts-from-multiple-custom-post-types/#post-1910111)
 * Guys, I fixed it by myself. Here’s the code with comments
 *     ```
       <!-- here starts the normal loop -->
       <?php query_posts( 'post_type=events'); ?>
       	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   
       	<!-- There's a count in the loop that starts at zero. After 3 he break's the loop -->
       	<?php static $count = 0;
       	if ($count == "3") { break; }
       	else { ?>
   
       		<!-- The normal loop -->
       		<div class="test">
       			<h1><?php the_title(); ?></h1>
       			<?php the_content(); ?>
       		</div>
   
       	<!-- before running the loop again, php adds 1 up in the count variable -->
       	<?php $count++; } ?>
       	<?php endwhile; ?>
   
       	<!-- Setting a if statement. If the loop is 1, then show another loop with an custom post type -->
       	<?php if ( $count == "1" ) { ?>
       		<?php query_posts( 'post_type=media&showposts=2'); ?>
       			<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
       				<div class="test">
       					<h1><?php the_title(); ?></h1>
       					<?php the_content(); ?>
       				</div>
       			<?php endwhile; ?>
       		<?php endif; ?>
       	 <?php } ?>
   
       	<!-- Setting a if statement. If the loop is 2, then show another loop with an custom post type -->
       	<?php if ( $count == "2" ) { ?>
       		<?php query_posts( 'post_type=media&showposts=1'); ?>
       			<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
       			     <div class="test">
       			     	<h1><?php the_title(); ?></h1>
       			     	<?php the_content(); ?>
       			     </div>
       			<?php endwhile; ?>
       		<?php endif; ?>
       	<?php } ?>
   
       <?php endif; ?>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Always three posts from multiple custom post types’ is closed to new 
replies.

## Tags

 * [count](https://wordpress.org/support/topic-tag/count/)
 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [custom-query](https://wordpress.org/support/topic-tag/custom-query/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 1 participant
 * Last reply from: [thiesenmurray](https://wordpress.org/support/users/thiesenmurray/)
 * Last activity: [15 years, 4 months ago](https://wordpress.org/support/topic/always-three-posts-from-multiple-custom-post-types/#post-1910111)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
