Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joglomedia/7688366 to your computer and use it in GitHub Desktop.
Save joglomedia/7688366 to your computer and use it in GitHub Desktop.
WordPress limit access to posts/pages by user roles
you can use such conditions to show private posts only to logged in users with role contributor. Now you only need to make post private to make that post available for contributor.
Ref: http://wordpress.stackexchange.com/questions/60582/limit-access-to-posts-pages-by-user-roles
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
$private = get_post_custom_values("private");
if (isset($private[0]) && $private == "true") {
if ( current_user_can( 'contributor' ) ) { //passing role to it may sometime not work
the_title();
the_content();
} else { // text to show instead the post
echo 'this post is private, only contributor can view it';
}
} else { // this is visible to all
the_title();
the_content();
}
endwhile;
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment