0

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++;

}
7
  • Is it for a main query? Please show the code you got so far?
    – birgire
    Commented Jan 13, 2014 at 12:03
  • birgire can u check the code and give ur idea?
    – Balas
    Commented Jan 13, 2014 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
    Commented Jan 13, 2014 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
    Commented Jan 13, 2014 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
    Commented Jan 13, 2014 at 13:00

1 Answer 1

3

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.

5
  • 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
    Commented Jan 13, 2014 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
    Commented Jan 13, 2014 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
    Commented Jan 13, 2014 at 16:10
  • 1
    @birgire : see the edit. I found one of my answers involving a UNION.
    – s_ha_dum
    Commented Jan 13, 2014 at 17:32
  • ok, thanks for the link. Hopefully the OP has now enough information to solve his problem. +1
    – birgire
    Commented Jan 13, 2014 at 19:13

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.