Private posts for logged in users

If you want to be able to display private posts only to registered users within the loop, I’m pretty sure you’ll enjoy this recipe: I’m explaining how to do it, with a code exemple you can re-use on your blog and display posts with the custom field of private only to users that are logged into your website.



  if ( have_posts() ) :
while ( have_posts() ) : the_post();
// the loop
$private = get_post_custom_values('private'); // read custom field
if ( isset($private[0]) && $private == 'true' ) {
if ( is_user_logged_in() ) {
// display private post, only logged users
} else {
// display public post, for every visitors

Leave a comment