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],
));
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.com/luetkemj/2023628

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.