我正在尝试执行一个合并查询,以从post
职位类型和我的events
自定义帖子类型。这个post
查询基于是否设置了特征图像,以及events
查询基于帖子是否选择了元值。一切正常,除了:The query only returns 5 posts, and they are not balanced in the sense that [depending on which post I set as Featured/Add Featured Image too] they take precedence over one another.
例如:如果我向post
post结果将是3个事件,2个事件。如果我在events
post类型结果将是4个事件,1个post。
以下是我用来查询和返回帖子的代码:
<?php
$post_types = get_post_types(\'\',\'names\');
$featuredEvents = get_posts(array(
\'post_type\' => \'events\',
\'post_status\' => \'publish\',
\'showposts\' => 6,
\'meta_key\' => \'_ecmb_featured_event\',
\'meta_value\' => \'on\'
));
$featuredNews = get_posts(array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'showposts\' => 6,
\'meta_key\' => \'_thumbnail_id\'
));
$mergedPosts = array_merge( $featuredEvents, $featuredNews );
$postIDs = array();
foreach( $mergedPosts as $item ) {
$postIDs[] = $item->ID;
}
$uniquePosts = array_unique($postIDs);
$posts = get_posts(array(
\'post__in\' => $uniquePosts,
\'post_type\' => $post_types,
\'post_status\' => \'publish\',
));
foreach( $posts as $post ) :
setup_postdata($post);
if( get_post_type() == \'events\' ) { ?>
This is a Featured Event
<?php } elseif (get_post_type() == \'post\') { ?>
This a post with the Featured Image set
<?php } endforeach; wp_reset_query(); ?>
有人能帮我吗?
Edit:
由于下面的答案,我找到了答案,并使用了以下代码:
$sliderQueryArgs = array(
\'posts_per_page\' => 6,
\'post_type\' => array(
\'post\',
\'events\'
),
\'orderby\' => \'post_date\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'_ecmb_featured_event\',
\'value\' => \'on\'),
array(
\'key\' => \'_thumbnail_id\',
\'compare\' => \'EXISTS\')
)
);
$sliderPosts = get_posts( $sliderQueryArgs );