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++;
}
'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.get_posts()
with two random post types, and order the result by the even/odd pattern?get_post_types()
andrand()
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)