Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm creating two different WP_Query objects, one for each of two different post types I'm using. In creating my output, I'd like to loop through one of the objects, and at the same time loop through the other object as well, so I can output pieces of them together. Here's an example of what I'd like to accomplish:

$rotator_args = array(
    'post_type' => 'rotator',
    );

$rotator = new WP_Query($rotator_args);

$numrotators = $rotator->post_count; 

$testimonial_args = array(
    'post_type' => 'testimonial',
    'posts_per_page' => $numrotators,
    );

$testimonial = new WP_Query($testimonial_args);

while ( $rotator->have_posts() ) : 
    $rotator->the_post(); //setup the rotator posts
    $testimonial->the_post(); //this breaks things, understandably
    $rotator_image = get_the_post_thumbnail($rotator->ID, 'large');
    $testimonial_image = get_the_post_thumbnail($testimonial->ID, 'large');
    $return .= '<div>' . $rotator_image . '</div>';
    $return .= '<div>' . $testimonial_image . '</div>';
endwhile;
return $return;

However, that doesn't work. Taking out $testimonial->the_post(); (and the associated $testimonial related code) fixes things.

share|improve this question
    
Not quite sure what you're aiming for here, functionality-wise. If you're trying to associate posts, why not use scribu's posts2posts plugin? scribu.net/wordpress/posts-to-posts –  ZaMoose Feb 7 '12 at 21:39
    
Nope, not trying to associate them. –  tnorthcutt Feb 7 '12 at 21:48

4 Answers 4

Go through each loop, storing the post object in an array. Then at the end combine them. (This is completely untested)

https://gist.github.com/1762204

share|improve this answer
    
Bill's got the right end of the stick on this one, methinks. Though it does seem a tad un-optimized. –  ZaMoose Feb 7 '12 at 21:44
    
At first glance this looks perfect. @ZaMoose, is there anything in particular that seems un-optimized? (asking so I can work on speeding it up if necessary/possible). –  tnorthcutt Feb 7 '12 at 21:50
    
This doesn't allow for using any functions that don't take a post ID (or other WP_Query property) as an argument, does it? If not, any suggestions on a way to implement something that does allow for using e.g. get_the_content()? –  tnorthcutt Feb 7 '12 at 21:54
    
You're essentially parsing through two arrays of objects and then doing the same thing over again -- you're duplicating your effort to avoid $post-related clobberings. I may have to give this some thought. –  ZaMoose Feb 7 '12 at 21:55
    
Right before the loop put print_r( $rotator_items[0] ); to see all the data you have. For instance, for the content use $rotator_items[0]->post_content –  Bill Erickson Feb 7 '12 at 22:24

I'm not sure you can merge the two loops together, since there isn't anything to keep them separate from a data standpoint. Are you trying to add data from a testimonial into the rotator as it goes?

share|improve this answer
    
Yes, trying to get data from the testimonial inside the rotator pane (actually using <li>s instead of <div>s, but I changed that to simplify things here). –  tnorthcutt Feb 7 '12 at 21:35

Edit: Bill's refactor is much better than my duct tape Edit: Additionally, my edit will stick with the same testimonial which wasn't even the desired outcome. Again: please see Bill's refactor

The trouble you're having is with $post. Every time you fire the_post() $post has the applicable data. I would suggest something like the following:

global $post;

$rotator_args = array(
    'post_type' => 'rotator',
    );

$rotator = new WP_Query($rotator_args);

$numrotators = $rotator->post_count; 

$testimonial_args = array(
    'post_type' => 'testimonial',
    'posts_per_page' => $numrotators,
    );

$testimonial = new WP_Query($testimonial_args);
if( $testimonial->have_posts() )
{
    $testimonial->the_post();
    $my_testimonial = $post;
}

while ( $rotator->have_posts() ) : 
    $rotator->the_post(); //setup the rotator posts
    $rotator_image = get_the_post_thumbnail($rotator->ID, 'large');
    $testimonial_image = get_the_post_thumbnail($my_testimonial->ID, 'large');
    $return .= '<div>' . $rotator_image . '</div>';
    $return .= '<div>' . $testimonial_image . '</div>';
endwhile;
return $return;
share|improve this answer
    
This won't work. It will continually return a single testimonial. –  ZaMoose Feb 7 '12 at 21:41
    
You're totally right, I didn't realize he was trying to grab a new one in each iteration. –  Jonathan Christopher Feb 7 '12 at 21:43

Is there some piece of data that connects $rotator to $testimonial? If not then I don't see what the point is…you just have two disparate loops.

share|improve this answer
    
Right, but I want them to run together, essentially, so that I can output information from them in a connected fashion Any one post in the rotator post type is not, however, connected to any one post in the testimonial post type. –  tnorthcutt Feb 7 '12 at 22:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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