How to Password Protect a Page or Post in WordPress

,

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.



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.


Hiding Password Protected Posts Completely From Front End

Add the following code in your functions.php file

// 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');





Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.