Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Hide Password Protected Posts From WordPress Loop

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

WordPress allows you to create password protected posts. Recently one of our readers asked if it was possible to hide password protected posts from the site. By default, WordPress hides the content of a password protected post, but it still shows the post title with ‘Protected’ prefix. In this article, we will show you how to hide password protected posts from the WordPress loop.

Why Hide Password Protected Posts in WordPress?

By default, WordPress displays the password protected post with its title and a ‘protected’ prefix. Users will need to enter the password to view the content of the post.

Password protected posts displayed on homepage and in widgets

This post title is visible on the homepage, archives, recent posts widget, etc. If you want to keep some content completely private, then this is not a ideal.

Not only users who don’t have password can see the post title, they can also try to enter passwords. As we all know, passwords can be cracked.

Having said that, let’s take a look at how to hide your password protected posts from WordPress loop so that other users cannot see them.

Hiding Password Protected Posts in WordPress

Simply add this code to your theme’s functions.php file or a site-specific plugin.

function wpb_password_post_filter( $where = '' ) {
    if (!is_single() && !is_admin()) {
        $where .= " AND post_password = ''";
    }
    return $where;
}
add_filter( 'posts_where', 'wpb_password_post_filter' );

This code simply modifies the query sent to the WordPress by using the posts_where filter. It asks WordPress to fetch all posts that do not have a password.

Visit your website and you will see that password protected posts are no longer visible on homepage, archives, or in widgets like recent posts.

Before and after hiding protected posts in WordPress

You can still visit the post by accessing it through a direct URL to the post itself.

The example above, hides password protected posts from all users. What if you ran a multi-author WordPress site and wanted protected-posts to be viewable by users with the capability to edit private posts?

Simply modify the above code with another conditional tag, like this:

function wpb_password_post_filter( $where = '' ) {
   if (!is_single() && !current_user_can('edit_private_posts') && !is_admin()) {
        $where .= " AND post_password = ''";
    }
    return $where;
}
add_filter( 'posts_where', 'wpb_password_post_filter' );

In this example, we check if a user cannot edit the password protected posts, then only show the posts that don’t have password. Doing so all users with user roles of administrator and editor will see the password protected posts on the front end of your site.

We hope this article helped you hide password protected posts from WordPress loop on your site. You may also want to see our tutorial on how to change private and protected posts prefix in WordPress.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

12 CommentsLeave a Reply

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Vincent Zhang says

    Thank you guys so much. This really helped me. I appreciate it. Please keep more of this type of posts coming that do not involve using a plugin.

  3. Mark says

    I created a site specific plugin and followed these steps and it worked for hiding my post on the “Posts” page of my site. However, the post is still visible on a Related Posts widget for each individual post. (Very similar to the image you have above, however my Password protected post is still visible.)

    Is there anything I can do to fix this?

  4. Alex says

    I wanted to make my own code adjustment to show the posts if you could read_private_posts.

    function remove_password_protected_posts( $where = ” ) {
    if (!is_single() && !current_user_can(‘read_private_posts’) && !is_admin() ) {
    $where .= ” AND post_password = ””;
    }
    return $where;
    }
    add_filter( ‘posts_where’, ‘remove_password_protected_posts’ );

    Great post as usual. Thanks.

  5. Harin says

    Hi guys

    I made a site specific plugin with the following code:

    If I try to attach a nextgen gallery to my post, the gallery doesn’t load, as soon as I disable the plugin, the nextgen gallery goes back to normal.

    Regards

  6. Brandon says

    Thanks for this snippet. So helpful!

    Regarding hiding these posts from the rss feed, I ran across this snippet.


    function rss_filter_protected($query) {
    if ($query->is_feed) {
    add_filter('posts_where', 'rss_filter_password_where');
    }
    return $query;
    }
    add_filter('pre_get_posts','rss_filter_protected');

  7. Chris says

    Thanks for that great tip!
    But are these posts hidden from the loop with your code snippet also hidden from the RSS feed?

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WPBeginner Assistant
How can I help you?

By chatting, you consent to this chat being stored according to our privacy policy and your email will be added to receive weekly WordPress tutorials from WPBeginner.