我已经在这个问题上旋转了很长时间并且我已经没时间了。所以我转向你,Stack Users。
某些背景信息:根据您选择查看网站的方式,此网站会忽略某些类别。中/高中用户会看到某些类别和帖子,成人教育用户会看到MS / HS学生不喜欢的帖子和类别。这个功能的第一部分有效。其余的没有,我无法弄清楚为什么。该功能的第一部分过滤掉必要的帖子,包括事件和视频。如果您在各自的类别页面上,则最后两个条件会恢复它们。或者,它应该。这就是意图。它不是。
function filter_posts( $query ) {
if ( ( $query->is_home() || is_category() ) && $query->is_main_query() ) :
if ( isset( $_COOKIE['branch'] ) ) :
$postsToExclude = array('15','10','16');
if ( $_COOKIE['branch'] == 'middle-high-school' ) :
array_push($postsToExclude, '69','36','70');
elseif ( $_COOKIE['branch'] == 'adult-education' ) :
array_push($postsToExclude, '35', '40', '68', '36','70');
endif;
$query->set( 'category__not_in', $postsToExclude );
endif;
endif;
if ( $query->is_front_page() && $query->is_main_query() ) :
// We also need to exclude the featured post from the main blog feed.
$custom_meta[] = array(
'key' => 'blog_feature_this',
'value' => '"Yes"',
'compare' => 'NOT IN'
);
$query->set('meta_query', $custom_meta);
endif;
if ( is_category( 'events' ) && $query->is_main_query() ) :
$query->set( 'category__in', array(10) );
endif;
if ( is_category( 'videos' ) && $query->is_main_query() ) :
$query->set( 'category__in', array(16) );
endif;
}
add_action( 'pre_get_posts', 'filter_posts' );
答案 0 :(得分:1)
尝试这样的事情 - 只使用一个参数并根据需要调整值数组。
function filter_posts( $query ) {
// Define the array
$postsToExclude = array();
if ( ( $query->is_home() || is_category() ) && $query->is_main_query() ) :
if ( isset( $_COOKIE['branch'] ) ) :
$postsToExclude = array('15','10','16');
if ( $_COOKIE['branch'] == 'middle-high-school' ) :
array_push($postsToExclude, '69','36','70');
elseif ( $_COOKIE['branch'] == 'adult-education' ) :
array_push($postsToExclude, '35', '40', '68', '36','70');
endif;
endif;
endif;
if ( $query->is_front_page() && $query->is_main_query() ) :
// We also need to exclude the featured post from the main blog feed.
$custom_meta[] = array(
'key' => 'blog_feature_this',
'value' => '"Yes"',
'compare' => 'NOT IN'
);
$query->set('meta_query', $custom_meta);
endif;
if ( is_category( 'events' ) && $query->is_main_query() ) :
// If the category is in the exclude list remove it
if(array_key_exists('10',$postsToExclude)) {
unset($postsToExclude['10'];
}
endif;
if ( is_category( 'videos' ) && $query->is_main_query() ) :
// If the category is in the exclude list remove it
if(array_key_exists('10',$postsToExclude)) {
unset($postsToExclude['16'];
}
endif;
// If there are some values
if(!empty($postsToExclude)) {
$query->set( 'category__not_in', $postsToExclude );
}
}
add_action( 'pre_get_posts', 'filter_posts' );