How to protect your posts or pages with a password

Looking for a way to publish content on your WordPress blog that’s only visible to certain people, instead of sharing every single post with the whole world? We’ve got you covered.

January 18, 2021

How to create a private page in WordPress

Have you ever wondered if there's a way to publish content on your WordPress blog that's only visible to certain people, instead of sharing every single post with the whole world?

If so, we’re happy to tell you that it's possible. You just need to learn about private pages in WordPress and how to create one using a password.

What is a private page in WordPress?

Many people ask what is a private page in WordPress, either because they see the options on their WordPress editor or they hear other people talking about it. A private page in WordPress is essentially a page or post that can only be read by selected visitors to your site. One of the easiest ways to make a private page in WordPress is by using a password.

What is a private page in WordPress that's password-protected?

As part of its content management system, WordPress comes with a number of basic content visibility options, but most novice users don't notice them.

While you write a new post or page and go to publish it, you'll see a heading "Visibility". By default, this is set to public, but if you click Edit then several other options will appear. One of them is "Password protected".

When you click "Password protected," you'll see an option to add a password to the post. Once you've done this, only visitors who have the right password will be able to read the post.

If you set your WordPress post to Private, then only users who have Editor or Admin status will be able to read it.

You can do the exact same thing for WordPress Pages.

Migrate to uPress within 24 hours

How to hide WordPress posts entirely

Although only selected visitors can read your private or password-protected posts, they still show up on your homepage and in search results, even though the average guy on the internet can’t read the actual content.

But there is a way to hide these posts entirely so that they don't even show up in search or on the homepage. It does involve a little code, but our step-by-step guide makes it easy.

  1. Copy the code below.
// 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');
  1. Go to your WordPress menu and hover over “Appearance.” In the menu that appears, click "Editor."
  2. On the right-hand side of the screen, you'll see "functions.php" or "Theme functions." Click this to go to the code editor.
  3. Now paste the code that you copied into the code editor and click Save.

That's all it takes. Now your private and protected posts will be totally hidden.

If you want to share a password-protected post with someone, just send them the URL of the post together with the password they need to access it. 

Now you know what is a private page in WordPress and how to create one easily.

As managed WordPress hosting experts, we know what we're talking about, whether you need simple information about private and password protected pages, or you want the whole lowdown on security and what you can do to protect your site. You can trust us with the entire gamut of WordPress questions, so the only one left is... why aren't we hosting your WordPress site yet? Click below and join us.

Explore plans

hello world!

Read more about these topics!

Related Posts

A WordPress Sandbox: What it is, Why You Need it and How to Use it

Have you ever spent hours updating your WordPress site only to have it crash, losing all your work? Don't you wish there were a place you could test changes to your WordPress site before taking them live? A WordPress sandbox allows you to play around with your site while removing the risk.
Read More

Migrate WordPress Website from Previous Host

Ready to move to uPress? This user-friendly guide will teach you how to migrate your site with our Auto Migration Tool - you can complete the process and be up and running in 24 hours!
Read More

Managed WordPress Hosting

Managed website hosting essentially means that the host takes care of all your website's technical needs. If you're paying for "regular" hosting, without any management specified in the price, you're likely just getting unmanaged hosting. What's the day-to-day difference, and why do you need managed WordPress hosting? Find out here.
Read More
envelopemap-marker