Hide password protected posts

Both the the_content() and the the_excerpt() template tags already account for password-protected posts, via the post_password_required() conditional. If you need to output content, or comments, etc. outside of the_content()/the_excerpt(), call the post_password_required() conditional directly.

For example, if you don’t want the comments template to be output if the post is password-protected. you could do the following:

if ( ! post_password_required() && ( is_single() || ( is_page() && comments_open() ) ) ) {          
    comments_template( '', true );
}

Or, if you don’t want to display the post at all if it is password-protected, you could do something like this, inside the Loop:

if ( post_password_required() ) {
    return;
} else {
    // Normal Loop Post output goes here
}

Leave a Comment