How to display posts from multiple post type in random order in wordpress?

You would need two queries to achieve this. You can’t specify the limit per post type in WP_Query (without modifying the raw SQL). What you could do is simply query the posts and the photos and merge the resulting arrays:

// Fetch posts
$posts_query = new WP_Query( array(
    'post_type'=> 'post',
    'orderby' => 'date',
    'posts_per_page' => 10,
    'paged' => get_query_var( 'paged' )
) );

// Fetch photos
$photos_query = new WP_Query( array(
    'post_type'=> 'photo',
    'orderby' => 'date',
    'posts_per_page' => 10,
    'paged' => get_query_var( 'paged' )
) );

// List of merged photos and posts
$mergedposts = array();

for ( $i = 0; $i < 10; $i++ ) {
    // Add post to list
    if ( isset( $posts_query->posts[ $i ] ) ) {
        $mergedposts[] = $posts_query->posts[ $i ];
    }

    // Add photo to list
    if ( isset( $photos_query->posts[ $i ] ) ) {
        $mergedposts[] = $photos_query->posts[ $i ];
    }
}

Then, you should set up the post data using setup_postdata (as you can’t use the_post outside of the query scope).

Leave a Comment