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!
One way is to customize the SQL query executed using
posts_clauses
or other such filters. To find them search forposts_clauses
in “wp-includes/query.php” & see the series of filters just before this line. These together are capable of customizing any part of the queryAnother thing you can do is manually merge the queried posts in the objects
@mridual aggarwal your answer is very very good but unfortuntly it is not really combining the 2
wp_query
it is only shows the posts from both in arrange i mean 5 posts from the first & 5 from the second but not sorted all in one so i have this solution & it exactly achieved the goal for my self at least