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
1 Answer
Reset to default 0If 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
版权声明:本文标题:plugin development - Hide post by 'post_category' 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745523530a2661736.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论