Revision 328 is a pre-publication revision. (Viewing current revision instead.)

Get latest 3 posts from multiple custom post types in one query

[php] <?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; ?> [/php]

Revisions

  • December 18, 2012 @ 13:48:29 [Current Revision] by PeterLugg
  • December 18, 2012 @ 13:46:58 by PeterLugg

Revision Differences

December 18, 2012 @ 13:46:58Current Revision
Content
 Added: [php]
 Added: &lt;?php
 Added: $args = array(
 Added: 'post_type' =&gt; array( 'type1', 'type2', 'type3' ),
 Added: 'posts_per_page' =&gt; -1
Deleted: Added: );
 Added: $myquery = new WP_Query( $args );
 Added: $type1 = 0; $type2 = 0; $type3 = 0; $count = 0;
 Added: while ( $myquery-&gt; have_posts() ) : $myquery-&gt;the_post();
 Added: if ( $post-&gt;post_type == 'type1' ) $type1++;
 Added: if ( $post-&gt;post_type == 'type1' &amp;&amp; $type1 &gt; 3 ) continue;
 Added: if ( $post-&gt;post_type == 'type2' ) $type2++;
 Added: if ( $post-&gt;post_type == 'type2' &amp;&amp; $type2 &gt; 3 ) continue;
 Added: if ( $post-&gt;post_type == 'type3' ) $type3++;
 Added: if ( $post-&gt;post_type == 'type3' &amp;&amp; $type3 &gt; 3 ) continue;
 Added: $count++; if ( $count &gt; 9 ) continue;
 Added: // Do Stuff
 Added: endwhile;
 Added: ?&gt;
 Added: [/php]

Note: Spaces may be added to comparison text to allow better line wrapping.

Tags: , ,

No comments yet.

Leave a Reply