How to Password Protect a Page or Post in WordPress

Have you ever thought about creating a blog post just for close friends or family members? Or publish a post just for editors on your WordPress site? As a content publishing platform, WordPress comes with some basic content visibility options that most beginners overlook. In this article, we will show you how to create a password protected page or post in WordPress.

When writing posts, there is an option called Visibility, which is set to Public by default. If you click Edit, then you will see options such as Password Protected and Private.

Making posts Private or Password Protected in WordPress

Simply click on Password Protected to add a password to your post. Doing so, only the friends, family members, or users you share this password with will be able to see this post.

A private post in WordPress is visible only to logged in users with the Editor or Administrator level user role in WordPress.

The same trick works for password protecting a page in WordPress as well.

Hiding Password Protected Posts Completely From Homepage and Archives

All your password protected posts may appear among rest of your posts on the homepage, or archives of your site. If you want to hide them completely, then use this code in your theme’s functions.php or in a site-specific plugin.

01 // Hide protected posts
03 function exclude_protected($where) {
04     global $wpdb;
05     return $where .= " AND {$wpdb->posts}.post_password = '' ";
06 }
08 // Where to display protected posts
09 function exclude_protected_action($query) {
10     if( !is_single() && !is_page() && !is_admin() ) {
11         add_filter( 'posts_where', 'exclude_protected' );
12     }
13 }
15 // Action to queue the filter at the right time
16 add_action('pre_get_posts', 'exclude_protected_action');

That’s all. Your protected posts will not appear in the site’s homepage or archives. You can send the post’s URL to users with the password.