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.

I've been asked to build a WordPress that limits access to posts with a particular custom post type based on the logged in users level. There will be a number of pre made user roles and they would like to choose which levels can view each post when the post is written.

What is the best way to do this?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

There are two ways to do this. Almost any decent membership management plugin will enable you to restrict content to a given user role and plugins such as Role Scoper are even more flexible. For a more code-based solution you would add some variation of the following to the desired theme template files where ever needed:

<?php if(current_user_can('some_capability') ); ?>

and replace some_capability with whatever preset or custom capability is present for the lowest desired role. For instance, to restrict to Editors and Admins (assuming no customization):

<?php if(current_user_can('delete_others_posts') ); ?>

To add this functionality to any given post in the custom post type is a bit more challenging. You would be getting involved in adding actions and functions similar to this Smashing Magazine tutorial to restrict posts by username. All you would need to do is change from getting a list of users to getting a list of roles or capabilities. Again, there is a world of plugins that do this already...if your needs are not complex then you may not wish to reinvent the wheel.

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.