抱歉,各位,对于那些好奇的人,我已经在稍后的函数中解决了这个问题。php文件,忘记关闭此过滤器(愚蠢)。如果有人想评论或改进,这里有一个更好的解决方案。基本上,我是根据用户角色(我也有一个自定义用户角色)过滤内容的,这样帖子就不会显示给不允许查看的用户。这必须包括侧边栏小部件(例如,post archive listings)和您可能看到文章标题或链接的所有其他位置:
/*-----------------------------------------------------------------------------------*/
/* Filter out users who shouldn\'t see content */
/*-----------------------------------------------------------------------------------*/
function filter_users($query){
if ( !is_admin() ) {
if( is_page() ) { return; }
$user_id = get_current_user_id();
$user_info = get_userdata($user_id);
$user_role = $user_info->roles[0];
$user_role = ( $user_role == \'subscriber\' || $user_role == \'author\' || $user_role == \'contributor\' || $user_role == \'editor\' ) ? \'member\' : $user_role;
if ( $user_role == \'member\' ) {
//wordpress needs a value to compare or it won\'t work
$query->set(\'meta_query\', array(
array(
\'meta_key\'=>\'who_can_view\',
\'meta_value\'=>\'foobar\',
\'compare\' => \'NOT EXISTS\',
)
)
);
add_filter( \'posts_join\' , \'custom_posts_join\' );
add_filter( \'posts_where\',\'custom_where\' );
}
}
}
add_filter(\'pre_get_posts\',\'filter_users\');
function custom_posts_join($join){
global $wpdb;
$join .= " LEFT JOIN $wpdb->postmeta as meta_1 ON $wpdb->posts.ID = meta_1.post_id";
return $join;
}
//Needed because WordPress craps the bed setting meta_query objects in pre_get_posts
function custom_where( $where = \'\' ){
global $wpdb;
$where .= " AND ( meta_1.meta_key = \'who_can_view\' AND meta_1.meta_value = \'member\' )";
remove_filter( \'posts_where\', \'custom_where\' );
return $where;
} // custom_where
干杯。