0

I'm building a post grid on my front page and I'd like to show one random post per CPT. At the moment, CPT are queried out in a query array:

<?php
$c = 1; //init counter
$bpr = 3; //boxes per row   
$wp_query = new WP_Query(array('showposts' => 9, 'orderby'=> 'rand', 'post_type' => array('productions',  'plays', 'movies', 'theatres', 'directors', 'artists', 'countries', 'events', 'grades')));
while( have_posts() ) : the_post(); ?>


<div class="post" id="post-<?php the_ID(); ?>">
   <p><?php $post_type = get_post_type_object( get_post_type($post) ); echo $post_type->label ; ?></p>
   <a href="<?php the_permalink() ?>" rel="bookmark">
      <?php if ( has_post_thumbnail() ) {
         the_post_thumbnail('thumbnail');
        } else { ?>
        <img src="<?php bloginfo('template_directory'); ?>/img/fallback_image.jpg" alt="<?php the_title(); ?>" />
      <?php } ?>
   </a>
</div>
<?php
if($c == $bpr) :
?>
<div class="clr"></div>
<?php
$c = 0;
endif;
?>
<?php
        $c++;
        endwhile;

?>

So the problem with this array is that it starts to dublicate the CPT's when I query them, sometimes even it queries 4 random posts from CPT.

One solution would be to create 9 different queries - one for each CPT, but this seems to be overkill for the front page.

Is there a way to really only query 1 random post per CPT in a way that wouldn't affect the page loading time so much?

1 Answer 1

0

This method might work for you. You will have to grab more posts than you need but it is better than doing 9 queries.

$args = array(
    'post_type' => array( 'type1', 'type2', 'type3', 'etc...' ),
    '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 > 1 ) continue;

    if ( $post->post_type == 'type2' ) $type2++; 
    if ( $post->post_type == 'type2' && $type2 > 1 ) continue;

    if ( $post->post_type == 'type3' ) $type3++; 
    if ( $post->post_type == 'type3' && $type3 > 1 ) continue;

    // I stopped at 3 but keep going as far as you need.

    $count++; if ( $count > 9 ) continue;

    // Do Stuff

    endwhile;
2
  • Thank you Chris_O! Well, it works, but one slight problem. On the index page it shows 8 different CPT's, and the 9th one is missing. I'm pretty sure i have made everything as it has to be: pastebin.com/TtEg2ipi
    – r1987
    May 29, 2012 at 14:12
  • Well this seems to be somekind of anomaly that i have 9 custom post types. When I query for 3, 6, 7, or 10 cpt's it all seems to work. But with 9, it doesn't query all the 9. When refreshing its swaping the last 2 CPT's that where queried out - showing only 8 .
    – r1987
    May 30, 2012 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.