Rewind posts is not necessary when you’re using new WP_Query() because it creates a new standalone query object. (i.e. it exists outside of the main query object, so the main one never ran and no rewinding is necessary)
This wouldn’t be the right use of rewind_posts() regardless – you have to follow up rewind_posts() with a new query every time so that you can start at the beginning.
The straightforward way to do this would be to create a new query object for each little category set you want to run.
<?php
$category12 = new WP_Query();
$category12 ->query('cat=12&showposts=2');?>
<?php while ($category12 ->have_posts()) : $original ->the_post(); ?>
<!--content-->
<?php } ?>
<?php endwhile; ?>
<?php
$category13 = new WP_Query();
$category13 ->query('cat=13&showposts=2');?>
<?php while ($category13 ->have_posts()) : $original ->the_post(); ?>
<!--content-->
<?php } ?>
<?php endwhile; ?>
etcetera...
Thats what i have now. Isnt that a lot of load for around 5 same queries? Am i still on the right track?
Your first loop is using the $original query, but every loop after that is only using the main query.
Here’s another option:
<?php
$original = new WP_Query();
$original ->query('cat=12,13,14');?>
<?php while ($original -> have_posts()) : $original ->the_post(); ?>
<?php if ( in_category('12') ) { ?>
<!--content-->
<?php } elseif ( in_category('13') ) { ?>
<!--content-->
<?php } elseif ( in_category('14') ) { ?>
<!--content-->
<?php } ?>
<?php } ?>
<?php endwhile; ?>
I haven’t tried it, but the loop should check all three conditions and behave accordingly with every post it goes through. No need for rewinding. Much simpler!
@that will only display the first category you called from in_category, what i want to achieve is to display all of these categories 2 each and going randomly.
I found a solution but the problem is i cant list it by twos with the category title because they act like a whole one set of queries so i cant combine the cat title and the two posts from the query
Functions*
function get_posts_for_cat_ordered( $cats = array(), $posts_per_cat = 2 ) {
if ( empty($cats) ) return false;
$args = array('posts_per_page' => 99, 'category__in' => $cats, 'orderby' => 'rand');
$q = new WP_Query( $args );
$posts_ordered = array();
$done = 0;
while ( $q->have_posts() ) : $q->the_post();
global $post;
$cats = get_the_category();
$cat = array_shift( $cats );
if ( ! isset($posts_ordered[$cat->slug]) )
$posts_ordered[$cat->slug] = array('term' => $cat);
if ( ! isset($posts_ordered[$cat->slug]['posts']) )
$posts_ordered[$cat->slug]['posts'] = array();
if ( count($posts_ordered[$cat->slug]['posts']) == $posts_per_cat ) {
$done++;
if ( $done == count($cats) ) return $posts_ordered;
continue;
}
$posts_ordered[$cat->slug]['posts'][] = $post;
endwhile;
wp_reset_postdata();
return $posts_ordered;
}
$cats = array(12, 13, 14);
$posts_per_category = 2;
$posts_ordered = get_posts_for_cat_ordered( $cats, $posts_per_category );
if ( ! empty($posts_ordered) ) { foreach ( $posts_ordered as $loop_data) {
// Example category heading
echo '<h2><a href="' . get_term_link($loop_data['term'], 'category') . '">' . $loop_data['term']->name . '</a></h2>';
global $post;
foreach ( $loop_data['posts'] as $post) {
setup_postdata($post);
?>
<!-- Example post content -->
<?php the_title(); ?><br />
<?php the_content(); ?><br />
<?php
}
wp_reset_postdata();
} }
Also it doesnt work on posts with two categories or more..
Ooh, sorry, didn’t catch that you wanted them random and I put it out too quickly. Excellent that you have it working the way that you want!