你可以创建一个函数并传入你想要的类型和数量,然后对它们运行一个 WP_Query,然后返回你的帖子。
例如
function so_getEqualPosts($number_posts, $post_types){
$postsToReturn = array();
foreach ($post_types as $post_type) {
$args = array(
'post_type' => $post_type,
'posts_per_page' => $number_posts,
'orderby' => 'date',
'order' => 'DESC'
);
$result = new WP_Query($args);
array_push($postToReturn, $result->posts);
}
usort($postsToReturn, function($a, $b) {
return strtotime($a['post_date']) - strtotime($b['post_date']);
});
return $postsToReturn;
}
** 以上更新以符合 OP 要求**
另一种方法是使用 StdClass;
$postsToReturn = new StdClass();
然后在每次迭代中添加到类中:
$postsToReturn->$post_type = $result->posts;
然后你可以调用它:
$posts = so_getEqualPosts(30, ["A", "B", "C"]);
然后应该可以通过以下方式访问帖子:
$posts->A
$posts->B
$posts->C
等等
这是未经测试的,而且非常动态,但应该给你一个起点:)