0

The situation is that I'm getting posts from 2 Custom Post Types which works perfectly fine. Below the code how I did that:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$blog_items = array(
    'post_type'=> array('tweet','post'),
    'paged' => $paged,
    'posts_per_page'=> 3,
    'status' => 'publish'
);
$posts = get_posts($blog_items);

This gives me back an array of 5 objects like I wanted like so:

Array(5){
  [0] Object( post_type:tweet )
  [1] Object( post_type:tweet )
  [2] Object( post_type:tweet )
  [3] Object( post_type:post )
  [4] Object( post_type:post )
}

But what I want to know is if I can loop it through each other like this:

Array(5){
  [0] Object( post_type:tweet )
  [1] Object( post_type:post )
  [2] Object( post_type:tweet )
  [3] Object( post_type:post )
  [4] Object( post_type:tweet )
}

I have no clue how to do it and if it's possible. So my question is how to do this. I hope I have provided enough and all help is appreciated,thanks!

1
  • try 'orderby' => 'rand' (because I don't think that is possible) or may be you can manipulate the array after getting the post object.
    – Puni
    Aug 11, 2015 at 13:01

2 Answers 2

1

I don't think that is possible through query but you can try 'orderby' => 'rand' or may be you can manipulate the array after getting the post object. Here is an example on how you can achieve that.

$foo = array("foo1","foo2","foo3","foo4","bar1","bar2","bar3","bar4");
var_dump($foo);
$newfoo = array();
for ($i=0; $i < count($foo)/2; $i++) { 
    array_push($newfoo,$foo[$i],$foo[count($foo)/2+$i]);
}
var_dump($newfoo);

enter image description here

1
  • Thanks, this works but it only works if it has the same amount of each type. If you there's 4 of one type and 7 of the other it won't generate correct.
    – Noob17
    Aug 17, 2015 at 13:02
0

First of thanks for all the help.

Conclusion: It seems impossible to generate an array by with 2 post custom types through each other (in the order like I put in the question) in Wordpress, so that goes out of the window. the only way to get this,is to manipulate the post objects or to merge arrays.

For those who might end in the same situation, this is the concept of the way how I fixed it:

1 . I created 2 seperate arrays of the post types:

$posts = get_posts('post_type=post');
$tweets = get_posts('post_type=tweet');

//This will be used in next step
$tweets_count = count(get_posts('post_type=tweet'));

2 . Then I loop the posts with post type:posts :

foreach( $posts as $key,$post ){
  echo '<div>' . $post->title . '</div>';

  //this will only show as many tweets as the Tweets array picks up
  if($key <= $tweets_count ){
    echo '<div>' . $tweets[$key]->title. '</div>';
  }
}

This allows one tweet for every post that's generated, the Tweet is accessed by the key of the post. If there are more posts than tweets it would obviously start showing empty divs when the Tweets run out. the if statement prevents that by showing the amount of Tweets in the Tweet array, this way it won't display anything once all Tweets have been displayed instead of empty divs.

It's not perfect but in my case this solution worked perfectly and the amount of posts and Tweet can be different and won't pose a problem in this solution.

Your Answer

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

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