Conditional to test if post has password protection enabled

I am looking for a conditional to test simply if a post (custom post type, if it somehow makes a difference) is set to have password protection…persistently…regardless of whether user has accessed the post via the password or not.

post_password_required() only returns true if the user has not already entered the password

get_post_status() (though it does return a ‘private’ status if a post is private) only returns ‘published’ for a password-protected post

1
1

You could use

if(!empty($post->post_password)){
   // do some stuff
}

which is what the post_password_required() code does before checking the user’s credentials against the password itself.

Leave a Comment