这里是另一种方法,特别是当您需要使用查询对象时。合并查询的问题是您失去了查询对象的正确性。
我的想法是运行两个独立的查询,一个非常精简的查询从date_query
查询并合并post ID的两个数组,然后将这些ID传递给适当的WP_Query
查询
(注意:此代码至少需要PHP 5.4)
$highlights = [8308, 8315];
$args = [
\'date_query\' => [
[
\'after\' => \'1 week ago\'
]
],
\'posts_per_page\' => 13,
\'meta_key\' => \'post_views_count\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\',
\'cat\' => \'-907,-908,-909\',
\'post__not_in\' => $highlights,
\'fields\' => \'ids\' // ONLY GET POST ID\'s, VERY LEAN QUERY
];
$q = get_posts( $args );
$merged_ids = array_merge( $highlights, $q );
请注意,高亮显示在查询的前面,来自$q的帖子是按日期排序的。如果需要保留此订单,只需添加
\'orderby\' => \'post__in\',
以下查询的参数
if ( $merged_ids ) {
$args_final = [
\'post__in\' => $merged_ids,
\'posts_per_page\' => -1,
\'orderby\' => \'post__in\',
\'ignore_sticky_posts\' => 1
];
$query_final = new WP_Query( $args_final );
// Run your loop
}