3

I have a an array of posts that are returned by doing three queries. 3 posts from the blog where posts are NOT in 'In the Media' or 'Insights', 3 from the blog where posts are in 'In the Media' 3 from the blog where posts are in 'Insights'.

Here's what I have for that. I don't think it's the most elegant solution:

<? $args = array(
    'post_type' => 'post',
    'posts_per_page' => 3,
    'category__not_in' => array( 268, 269 )
  );
$homePosts = new WP_Query($args);

$args = array(
    'post_type' => 'post',
    'category_name' => 'in-the-media',
    'posts_per_page' => 3
  );
$inthemediaPosts = new WP_Query($args);

$args = array(
  'post_type' => 'post',
  'category_name' => 'bt-insights',
  'posts_per_page' => 3
);
$insightsPosts = new WP_Query($args);

$allqueries = array($homePosts,$inthemediaPosts,$insightsPosts);
foreach ($allqueries as $myquery) {
  while ($myquery->have_posts()) : $myquery->the_post(); ?>

Currently, that loops through 3 homeposts, then 3 inthemedia posts, then 3 bt-insight posts.

What I need is for the loop to go through 1 homepost, 1 inthemedia post, 1 bt-insight post, then 1 homepost, 1 inthemediea post... etc repeat.

Hope that makes sense. Suggestions?

johnnyriss
  • 43
  • 1
  • 4
  • Please have a look at the custom user function [array_zip_merge()](http://stackoverflow.com/questions/1860490/interleaving-multiple-arrays-into-a-single-array) described in this question, this will allow you to merge three different arrays alternating the values from each array. This approach would be more than sufficient for what you're wanting to achieve – Scuzzy Sep 05 '12 at 23:19
  • @Scuzzy, `$myquery` doesn't seem to be an array, though... – Mischa Sep 05 '12 at 23:24
  • I've assumed that the following can be done: foreach(array_zip_merge($homePosts,$inthemediaPosts,$insightsPosts) as $myquery) – Scuzzy Sep 05 '12 at 23:26
  • Looking at the WordPress codex you may need to compile three separate arrays of your posts ahead of time to then be merged. (sorry I can no longer edit my previous comment) – Scuzzy Sep 05 '12 at 23:40

2 Answers2

0
while($allqueries[0]->have_posts() || $allqueries[1]->have_posts() || $allqueries[2]->have_posts()) {
  if ($allqueries[0]->have_posts()) $allqueries[0]->the_post();
  if ($allqueries[1]->have_posts()) $allqueries[1]->the_post();
  if ($allqueries[2]->have_posts()) $allqueries[2]->the_post();
}
Sam Grondahl
  • 2,397
  • 2
  • 20
  • 26
0

What if you remove the allqueries stuff and after:

$insightsPosts = new WP_Query($args);

Just use this instead.

for ($i = 0; $i < 3; $i++) {
    if ($homePosts->post_count > $i)
        echo $homePosts->posts[$i]->post_title;
    if ($inthemediaPosts->post_count > $i) 
        echo $inthemediaPosts->posts[$i]->post_title;
    if ($insightsPosts->post_count > $i) 
        echo $insightsPosts->posts[$i]->post_title;
}

That should just print out the title of the post and you can use the other fields as needed. This could be moved to a function to avoid duplicating the logic as well.

wes
  • 1,643
  • 2
  • 15
  • 12