admin管理员组

文章数量:1429064

I've found a way to hide posts by matching the current user meta key value with a list of an authors pre-approved meta values for a specific user field. First_name for example.

The code redirects any current user who doesn't match the authors list of meta values. This works well.

What I'd like to do is give the ability for authors to make some of their posts accessible to all by a selecting either a 'public' category or a 'private' category.

Currently the current user can't see any posts from an author they don't match meta values with. With this fix, they would be able to see any posts that aren't in the category 'private'. BTW, all posts are viewable by login only.

This is the part of my code that restricts the access to posts. I've added in post_category. I've tried the category name, the category ID and an array as below, even though I only want to create a match with one category.

How can I include a specific category in the restriction?

$myposts = get_posts(array(
    'p' => $pid,
    'posts_per_page' => -1,
    'post_type' => 'post',
    'post_category' => array('4'),
    'post_status' => 'publish',
    'author' => $value[0],
));

I've found a way to hide posts by matching the current user meta key value with a list of an authors pre-approved meta values for a specific user field. First_name for example.

The code redirects any current user who doesn't match the authors list of meta values. This works well.

What I'd like to do is give the ability for authors to make some of their posts accessible to all by a selecting either a 'public' category or a 'private' category.

Currently the current user can't see any posts from an author they don't match meta values with. With this fix, they would be able to see any posts that aren't in the category 'private'. BTW, all posts are viewable by login only.

This is the part of my code that restricts the access to posts. I've added in post_category. I've tried the category name, the category ID and an array as below, even though I only want to create a match with one category.

How can I include a specific category in the restriction?

$myposts = get_posts(array(
    'p' => $pid,
    'posts_per_page' => -1,
    'post_type' => 'post',
    'post_category' => array('4'),
    'post_status' => 'publish',
    'author' => $value[0],
));
Share Improve this question edited May 4, 2019 at 18:03 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked May 4, 2019 at 16:53 DomDom 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If you want to get the posts with that category

$myposts = get_posts(array( 'p' => $pid, 'posts_per_page' => -1, 'post_type' => 'post', 'category__in' => array( 4 ), 'post_status' => 'publish', 'author' => $value[0], ));

If you want to get the posts without that category

$myposts = get_posts(array( 'p' => $pid, 'posts_per_page' => -1, 'post_type' => 'post', 'category__not_in' => array( 4 ), 'post_status' => 'publish', 'author' => $value[0], ));

Take a look at this, may helps you in the future: https://gist.github/luetkemj/2023628

本文标签: plugin developmentHide post by 39postcategory39