我正在运行一个查询,根据标签挖掘相关帖子。如果当前帖子有标签A、B和C,那么再显示3篇有A、B或C标签的帖子。我也只显示365天或更新的帖子。
$date_to_compare = date(\'F jS, Y\', strtotime(\'-365 days\'));
$new_category = get_the_category();
$the_tags = get_the_tags();
$first_tag = $the_tags[0]->term_id;
$second_tag = $the_tags[1]->term_id;
$this_post2 = get_the_ID();
$args2 = array(
\'posts_per_page\' => 3,
\'post_type\' => \'post\',
\'ignore_sticky_posts\' => 1,
\'orderby\' => \'rand\',
\'tag__in\' => array( $first_tag, $second_tag ),
\'exclude\' => $this_post2,
\'date_query\' => array(
array(
\'after\' => $date_to_compare,
\'inclusive\' => true,
),
),
);
$the_query_2 = new WP_Query( $args2 );
if ($the_query_2->have_posts()) :
while ( $the_query_2->have_posts() ) : $related_post_count++;
$the_query_2->the_post(); ?>
// show the post content
endwhile; endif;
这通常效果很好,但有一种情况是,当前帖子是与此匹配的3篇帖子之一。在这种情况下,现有员额包括在三个相关员额中,尽管
$this_post2 = get_the_ID();
和
\'exclude\' => $this_post2,
因此,我的难题。
最合适的回答,由SO网友:jdm2112 整理而成
WP\\U查询不支持exclude
论点要按ID排除帖子,请使用post__not_in
并向其传递一个ID数组。
使用上面的代码,您的参数可能如下所示:
$args2 = array(
\'posts_per_page\' => 3,
\'post_type\' => \'post\',
\'ignore_sticky_posts\' => 1,
\'orderby\' => \'rand\',
\'tag__in\' => array( $first_tag, $second_tag ),
\'post__not_in\' => array( $this_post2 ),
\'date_query\' => array(
array(
\'after\' => $date_to_compare,
\'inclusive\' => true,
),
),
);