我想做这样的事情,但我想不出来。[单击][1]
我正在使用此自定义WP\\U查询在我的主页上显示即将发生的事件。
$events_args = array(
\'post_type\' => VA_EVENT_PTYPE,
\'posts_per_page\' => $number,
\'no_found_rows\' => true,
\'post_status\' => \'publish\',
\'ignore_sticky_posts\' => true,
\'tax_query\' => array(
array(
\'taxonomy\' => VA_EVENT_DAY,
\'field\' => \'slug\',
\'terms\' => $days,
\'include_children\' => false,
),
),
\'meta_key\' => VA_EVENT_DATE_META_KEY,
\'orderby\' => \'meta_value\',
\'order\' => \'asc\'
);
$r = new WP_Query( $events_args );
if ( $r->have_posts() ) :
我现在需要做的是:从这个查询中排除最近更新的2篇帖子,因为这些帖子已经显示在其他地方了。我不能使用多个循环,因为这是一个小部件,另一个查询位于另一个小部件中。非常感谢您的帮助!
最合适的回答,由SO网友:Philip 整理而成
找到了一个有效的解决方案:在实际查询之前,我有一个预查询,它确定已经在其他地方显示的帖子的ID,并将它们粘贴到一个变量中
// EXTRA QUERY TO DETERMINE WHICH EVENTS HAVE ALREADY BENN DISPLAYED BY THE UPDATED EVENT WIDGET
$events_args = array(
\'post_type\' => VA_EVENT_PTYPE,
\'posts_per_page\' => 2,
\'no_found_rows\' => true,
\'post_status\' => \'publish\',
\'ignore_sticky_posts\' => true,
\'orderby\' => \'modified\',
\'order\' => \'DESC\',
);
$biglocal_events_already_displayed = array();
$r = new WP_Query( $events_args );
if ( $r->have_posts() ) :
while ( $r->have_posts() ) : $r->the_post();
$biglocal_events_already_displayed[] = get_the_ID();
endwhile;
// Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
endif;
// END EXTRA QUERY
在实际查询中,我将这些帖子排除在外
$events_args = array(
// Other stuff
\'post__not_in\' => $biglocal_events_already_displayed
);
Puh。。。。成功了。