您需要两个查询来实现这一点。无法在中指定每个帖子类型的限制WP_Query
(无需修改原始SQL)。您只需查询帖子和照片并合并生成的数组:
// 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 ];
}
}
然后,您应该使用
setup_postdata
(因为您不能使用
the_post
超出查询范围)。