我正在尝试这样做:在主页(首页)上,我有多个帖子循环。我需要帖子不要在这些循环中重复(也就是说,如果帖子显示在第一个循环中,它就永远不会出现在第二个或第三个循环中,以此类推)。
I\'v想出了这些(较低),但我的解决方案要求我对每个循环执行2次查询(因此,如果我有3个循环,我将执行6个db请求),这有点糟糕。
所以我的问题是:我能在里面得到帖子ID吗pre_get_posts
滤器
迄今为止我的代码:
/** Hack to exclude repeating of posts */
if( ! function_exists( \'jews_exclude_shown_posts\' ) ) {
function jews_exclude_shown_posts( $query ) {
if( is_admin() ) return $query;
if( $query->is_main_query() ) {
$GLOBALS[\'jews_exclude\'] = array();
} elseif (
false !== $query->query_vars[\'jews_exclude\'] &&
(
null === $query->query_vars[\'post_type\']
|| ( is_array( $query->query_vars[\'post_type\'] ) && in_array( \'post\', $query->query_vars[\'post_type\'] ) )
|| \'post\' === $query->query_vars[\'post_type\']
)
) {
if( ! empty( $GLOBALS[\'jews_exclude\'] ) ) {
$query->set(\'post__not_in\', $GLOBALS[\'jews_exclude\']);
}
$args = $query->query;
$args[\'jews_exclude\'] = false;
$execute = new WP_query( $args );//here i do dubble query witch i dont\'t want to do
if( $execute->have_posts() ) {
foreach( $execute->posts as $ex )
$GLOBALS[\'jews_exclude\'][ $ex->ID ] = $ex->ID;
}
}
return $query;
}
}
add_filter( \'pre_get_posts\', \'jews_exclude_shown_posts\' );