3

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.

2 Answers 2

2

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>';
    }
}
1

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

3
  • 1
    Yeah and has been evolving much since i posted there.
    – Bainternet
    Jun 18, 2011 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, 2011 at 17:21
  • And vice versa: It would save us a lot of time if you check it yourself :)
    – kaiser
    Jun 18, 2011 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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