How to Password Protect a Page or Post in WordPress

121

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.

    // Hide protected posts    function exclude_protected($where) {  	global $wpdb;  	return $where .= " AND {$wpdb->posts}.post_password = '' ";  }    // Where to display protected posts  function exclude_protected_action($query) {  	if( !is_single() && !is_page() && !is_admin() ) {  		add_filter( 'posts_where', 'exclude_protected' );  	}  }    // Action to queue the filter at the right time  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.

We hope this article helped you password protect posts in WordPress. You may also want to check out how to make a whole WordPress site password protected without user registration.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.