而不是使用posts_where
最好使用pre_get_posts
滤器以下是我最终实现的代码:
add_filter( \'pre_get_posts\', \'hide_unwanted_posts_filter\' );
function hide_unwanted_posts_filter( $query ) {
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;
$key = \'unwanted_posts\';
$unwanted_posts = get_user_meta($user_id,$key,true);
if(is_user_logged_in() && !empty($unwanted_posts) && !$query->is_admin) {
$query->set(\'post__not_in\', $unwanted_posts ); // id of page or post 21753
}
return $query;
}
如果有人读了这篇文章,仍然想使用
posts_where
滤器下面是使用此其他筛选器的相同代码(简化):
add_filter( \'posts_where\',\'hide_unwanted_posts_filter\', 1, 2 );
function hide_unwanted_posts_filter($where, $wp_query = NULL) {
global $wpdb;
$wp_posts = $wpdb->prefix . "posts";
if ( !$wp_query ) {
global $wp_query;
}
if (is_user_logged_in() && !$wp_query->is_admin) {
$where .= " AND $wp_posts.ID NOT IN (22, 3)";
}
return $where;
}