How to Set Password to Posts and Pages in WordPress

WordPress is a content publishing platform so it comes with the basic content visibility options. Sadly most of the WordPress users are unaware of the fact that they can protect the privacy of their special posts. By simply changing the visibility, you can make your posts and pages private or password protected. In this article, we will show you how to set password to posts and pages in WordPress.

How to Set Password to Posts and Pages in WordPress

When you publish a post or page, it is publicly visible by default. You can easily change the visibility of post by following these easy steps.

  1. In the publish box of the edit post screen, you can find an option called Visibility. Clicking the ‘Edit’ link next to the Visibility: Public.
  2. You will see the three radio buttons after you click the edit link.
    • Public: Public is the first radio button option. This is set by default. If a post or page is set to public it will be visible to everyone.
    • Password Protected: The second option is password protected. When you select this radio button, you get a text field for entering a password. This password will be required to view the post.
    • Private: This option makes your post visible to only the site’s administrator and editors.
  3. Once you are done selecting the post visibility, click OK.
  4. Now update or publish the post, to save the visibility setting changes.

edit-button - How to Set Password to Posts and Pages in WordPress

Now if you have successfully changed your visibility to password protected and set a password to your post than your visitors will have to enter the password to read your post.

password- How to Set Password to Posts and Pages in WordPress

Finding or Changing a Password

Changing the posts password is similar to creating the password. You can change or find the password of any post by using the same “Visibility: Edit” link. You can also use the quick edit link on the All Posts Screen.

If you have forgotten the password of a particular post or page then you can simply visit the post’s edit page and click the edit link next to the visibility option.

password- How to Set Password to Posts and Pages in WordPress

Hiding Password Protected Posts from Homepage and Other Pages

Your password protected posts will show up on homepage and other places of your site. If you want to hide these protected posts from appearing in homepage or archive page then you may have to enter the following code in your functions.php file or site-specific plugin file. The good thing about it is that it does not affect the pagination too.

<?php

// Filter to hide protected posts
function exclude_protected($where) {
	global $wpdb;
	return $where .= " AND {$wpdb->posts}.post_password = '' ";
}

// Decide where to display them
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');

?>

Now your post will disappear from your site and random visitors cannot see it. For specific viewers, you can send the URL of your post and pages along with the password.

Also Read:   How to Uninstall WordPress Plugin in Proper Way

That’s all. We hope this article on “How to set password to posts and pages in WordPress” was helpful to you. You can also read our other article on “Steps to Reset WordPress Password from phpMyAdmin“.

The following two tabs change content below.
Kantiman Bajracharya

Kantiman Bajracharya

Kantiman Bajracharya is a freelance web developer. He is a computer geek who has a Bachelor’s degree in Computer Engineering. He has earned lots of satisfied customers while working for numerous successful projects. He is also a WordPress theme creator and web article writer. On free time he likes reading philosophy books, traveling and playing chess.

Share This Post