Skip to content

Instantly share code, notes, and snippets.

@c3mdigital
Created May 11, 2012 22:12
Show Gist options
  • Save c3mdigital/2662723 to your computer and use it in GitHub Desktop.
Save c3mdigital/2662723 to your computer and use it in GitHub Desktop.
Custom $wp_query loop to show 3 posts from 3 different post types using only 1 query
<?php
$args = array(
'post_type' => array( 'type1', 'type2', 'type3' ),
'posts_per_page' => -1
);
$myquery = new WP_Query( $args );
$type1 = 0; $type2 = 0; $type3 = 0; $count = 0;
while ( $myquery->have_posts() ) : $myquery->the_post();
if ( $post->post_type == 'type1' ) $type1++;
if ( $post->post_type == 'type1' && $type1 > 3 ) continue;
if ( $post->post_type == 'type2' ) $type2++;
if ( $post->post_type == 'type2' && $type2 > 3 ) continue;
if ( $post->post_type == 'type3' ) $type3++;
if ( $post->post_type == 'type3' && $type3 > 3 ) continue;
$count++; if ( $count > 9 ) continue;
// Do Stuff
endwhile;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment