Have you ever thought about creating a post just for close friends or family members? Or publishing a post only for editors on your WordPress site? As a content publishing platform, WordPress comes with a few basic content-visibility options that most beginner users ignore. In this article, we will show you how to protect a page
As a content publishing platform, WordPress comes with a few basic content visibility options that most beginner users ignore.
In this article, we will show you how to protect a page or post in WordPress with a password.
While writing the post, there is an option called Visibility, defined as: Public by default.
When you click on Edit, options such as the following will appear Password protected, or Private.
Original Hebrew uPress control panel. Follow the English instructions in this article; labels and positions may differ in the current interface.
To perform this action, simply click Password protected in order to add a password to a post.
That way, only friends, family members, or users with whom you share the password will be able to see the post.
A private post in WordPress is visible only to users who are logged in with User role at the Editor or theAdmin level in WordPress.
The same process also works for password-protecting a WordPress page.
Completely hiding password-protected posts on the home page and in search results
All of your password-protected posts may appear together with the rest of your posts on the Home, or in search results on the site.
If you want to hide them completely, you can embed the following code in thefunction.php of your WordPress site.
// 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 it, your protected posts will not appear on the home page or in the site's search results.
To publish a password-protected post to a user – you can send the post's URL together with the password.
We hope we have clarified how to protect your posts with a password, easily and effectively.
