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 am currently working in a project in wordpress.

Here i have to get all posts from two custom post types namely pt1, pt2.

But here i want to display posts with random post type(pt1, pt2).

Example)

I want to display first post from pt1, next from pt2 and again pt1 and next pt2 and so on...

How can i do this. Any help? This is the code:

 $args = array(

'posts_per_page'   => 5,    

'orderby'          => 'post_date',

'order'            => 'DESC',   

'post_type'        => array('pt1', 'pt2'),  

'post_status'      => 'publish'

 ); 

$posts = get_posts( $args );

$num=1;

foreach($posts as $post) {

    if( $num % 2 == 0 )             

        echo $post->post_title.$post->post_type;

    else    

        echo $post->post_title.$post->post_type;

    $num++;

}
share|improve this question
    
Is it for a main query? Please show the code you got so far? –  birgire Jan 13 at 12:03
    
birgire can u check the code and give ur idea? –  balamurugan Jan 13 at 12:44
    
What about 'orderby' => 'rand' instead? But I don't follow your random criteria. You want to order by random post type but still have it in even/odd order. –  birgire Jan 13 at 12:49
    
... or you want to feed the get_posts() with two random post types, and order the result by the even/odd pattern? –  birgire Jan 13 at 12:56
    
The functions get_post_types() and rand() might help you with the first part of selecting two random post types. But since you're ordering by post date, you might only get results from a single post type (e.g. p1) –  birgire Jan 13 at 13:00

1 Answer 1

There is no easy query that will do that (see below). You will need to process your results in PHP to get the sort you are looking for. Something like this:

$args = array(
  'posts_per_page'   => 5,    
  'orderby'          => 'post_date',
  'order'            => 'DESC',   
  'post_type'        => array('post', 'book'),  
  'post_status'      => 'publish'
); 
$posts = get_posts( $args );

$even = $odd = array();
$ei = 0;
$oi = 1;
foreach ($posts as $p) {
  if ('post' == $p->post_type) {
    $odd[$oi] = $p;
    $oi = $oi + 2;
  } elseif ('book' == $p->post_type) {
    $even[$ei] = $p;
    $ei = $ei + 2;
  }
}
$posts = $odd + $even;
ksort($posts);
foreach($posts as $post) {     
  echo $post->post_title.$post->post_type;
  echo '<br>';
}

Obviously, I used post types that exist on my dev server, but swapping those out is trivial. The idea is the same.

The only way I know to make sure you get equal numbers of each post type in the query would be to create a UNION. I don't have the time write that now but option 4 from this answer should get you started.

share|improve this answer
    
I guess we should also consider the case where the number of posts of post type p1 is much greater than posts of p2. So I suggested using two get_posts() to ensure we get enough posts of each post type. Maybe you know of other way around that? –  birgire Jan 13 at 15:40
    
Yes, that is an issue. The code above will interlace the two, so long as there are enough results in the query. With some effort you can cause WP_Query to use a UNION. I have an answer I should look up showing how. For the record, your suggestion was the first thing that came to mind, but I didn't want to steal it :) –  s_ha_dum Jan 13 at 15:55
    
Good idea to try UNION. Our initial idea might not scale well using n x get_posts() calls for n post types, but I guess the normal use case would be for low values of n ;-) –  birgire Jan 13 at 16:10
1  
@birgire : see the edit. I found one of my answers involving a UNION. –  s_ha_dum Jan 13 at 17:32
    
ok, thanks for the link. Hopefully the OP has now enough information to solve his problem. +1 –  birgire Jan 13 at 19:13

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.