WordPress has a default feature to add password protected posts or pages.

I guess you already know about this feature.

Here you will learn to hide passwords protected from the homepage and archives.

Why Hide Password Protected Posts in WordPress?

Though you create a password protected post, It is visible on homepage, archives,recent post etc with its title and a ‘protected’ prefix as shown in the image.

Everyone can see the post title and try to crack the password.

For certain members who visit the site and have a password can only see the post title. So you should hide password protected posts in WordPress.

Hiding Password Protected Posts Completely From Homepage and Archives

  • Login to wp-admin
  • Go to Appearance on the left sidebar 
  • Within Appearance, there is a sub menu called theme editor and click it.
  • Now find the “Functions.php” file.

There you need to copy the following code and paste it in “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'); 

After pasting code and updating the file, go to Homepage and Archives page, there you cannot see protected posts or pages title as shown in image.

Now you can share post’s url to your selected member with password.

Hope this article helps you to hide passwords protected from homepage and archives.

If not then comment below, we will try to reply to your queries as soon as possible.

Finally please share this article if it is a useful article for you.

Leave a Reply

Your email address will not be published. Required fields are marked *