Quick answer: post_password_required( $post ) tells you if a post is password protected and the visitor hasn’t entered the password yet. To check whether a post has a password at all, test ! empty( $post->post_password ).
I wrote the first version of this in 2010 for a theme that showed excerpts of protected posts by mistake. These two checks are still the right tools.
Hide content until the password is entered
if ( post_password_required() ) {
echo get_the_password_form();
return;
}
the_content();
Use this in custom templates or anywhere you output post content yourself, such as custom fields or a hero image, because WordPress only protects the_content() automatically.
Check if a post has a password set
$post = get_post( 123 );
if ( $post && '' !== $post->post_password ) {
echo 'This post is password protected.';
}
This is true even after the visitor has unlocked it, which makes it useful for badges like “Members only”.
Leave protected posts out of a query
$query = new WP_Query( array( 'post_type' => 'post', 'has_password' => false, ) );
Things that are not protected
Custom fields, featured images and REST API meta are not hidden by the post password. Wrap them in the same post_password_required() check.
FAQ
How long does the unlock last?
WordPress sets a cookie that lasts 10 days by default. You can change it with the post_password_expires filter.
Is a post password real security?
It’s fine for casual privacy, but for paid or sensitive content use proper user accounts or a membership plugin.
Updated December 2026. Need a hand with custom WordPress code like this? See my WordPress development services or contact me.