Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How to make a post visible to only:

  1. The post author, irrespective of its user role.

  2. All users from a specific user role.

Other then these, no one should be able to see the post.

How should I approach this.

Note that I have only three user roles in WP. Admin, and 2 other custom roles.

share|improve this question
add comment

2 Answers

We already got a plugin in the WPSE Plugin Repository written by BAinternet, that does exactly that.

share|improve this answer
1  
Yeah and has been evolving much since i posted there. –  Bainternet Jun 18 '11 at 15:07
1  
Thanks for the link. But I want to apply the condition globally so that I don't have to select the roles for each post. I am checking the code of the plugin to learn how to do that. But if you guys can post the snippet, it will save me some time :) Thanks for help! –  AgentWP Jun 18 '11 at 17:21
    
And vice versa: It would save us a lot of time if you check it yourself :) –  kaiser Jun 18 '11 at 18:57
add comment

Give your custom roles the capability to "read_member_posts" or whatever. Then you could apply a filter to the_content()

add_filter( 'the_content', 'my_wpse20347_filter' );

function my_wpse20347_filter( $content )
{
    global $post;
    if( author_can( $post->ID, 'edit_posts' ) || current_user_can( 'read_member_posts' ) )
    {
       return $content;
    }
    else
    {  // Everyone else sees this in place of the content.
       return '<p>Only members may view this post</p>';
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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