I am building a section on a site where I am merging two different post types into one loop, and then displaying them randomly. The problem is, I'm having a hard time finding a way to limit the amount of posts per type.
Here's what I've tried:
One query with multiple post types can be achieved with an array:
$args = array( 'post_type' => array( 'photos', 'quotes' ), ...
... but cannot limited to a certain number of posts per type.
Merging two query argument arrays before running WP_Query on it:
$photos = array( 'post_type' => 'photos', 'posts_per_page' => 15, 'orderby' => 'rand' ); $quotes = array( 'post_type' => 'quotes', 'posts_per_page' => 5, 'orderby' => 'rand' ); $args = $photos + $quotes; // Also tried array_merge( $photos, $quotes );
No luck on this. What happens is the latter variable
$quotes
overwrites$photos
and only shows the quotes.Merging two WP_Query objects together through typecasting:
$photos_query = new WP_Query( $photos ); $quotes_query = new WP_Query( $quotes ); $result = (object)array_merge( (array)$photos_query, (array)$quotes_query );
... and so on.
I could probably use an SQL query straight to the database, but I need to be able to combine these two separate post types for one loop, arranged randomly, AND limited to a certain amount of posts per type.
Thanks for your help!