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?
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:
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):
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.