How can I hide posts that are in an array of ids in the usermeta
for the current user in the main query
.
I can use post__not_in
in meta_query
but I do not know which option to use for only a specific user.
I think should use posts_where
?
How can I hide posts that are in an array of ids in the usermeta
for the current user in the main query
.
I can use post__not_in
in meta_query
but I do not know which option to use for only a specific user.
I think should use posts_where
?
I'm not entirely sure what the problem is, because you've already mentioned all the tools that you need to solve it...
Just use pre_get_posts
filter, check if the user is logged in, get the IDs of posts he should not see and exclude them in query:
function remove_some_posts_for_user( $query ) {
if ( ! is_admin() && is_user_logged_in() && $query->is_main_query() ) {
$posts_to_remove_for_current_user = get_user_meta( get_current_user_id(), 'posts_to_remove', true );
if ( ! empty($posts_to_remove_for_current_user) is_array($posts_to_remove_for_current_user) ) {
$query->set( 'post__not_in', $posts_to_remove_for_current_user );
}
}
}
add_action( 'pre_get_posts', 'remove_some_posts_for_user' );