BlogTutorials

How to Hide Password Protected Posts From WordPress Loop

WordPress permits you to create password protected posts. Just lately considered one of our readers requested if it was doable to cover password protected posts from the location. By default, WordPress hides the content material of a password protected submit, nevertheless it nonetheless exhibits the submit title with ‘Protected’ prefix. On this article, we’ll present you easy methods to disguise password protected posts from the WordPress loop.

Why Disguise Password Protected Posts in WordPress?

By default, WordPress shows the password protected post with its title and a ‘protected’ prefix. Customers might want to enter the password to view the content material of the submit.

Password protected posts displayed on homepage and in widgets

This submit title is seen on the homepage, archives, latest posts widget, and many others. If you wish to maintain some content material utterly personal, then this isn’t a best.

Not solely customers who don’t have password can see the submit title, they will additionally attempt to enter passwords. As everyone knows, passwords could be cracked.

Having stated that, let’s check out easy methods to disguise your password protected posts from WordPress loop in order that different customers can not see them.

Hiding Password Protected Posts in WordPress

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

1
2
3
4
5
6
7
operate wpb_password_post_filter( $the place = '' ) {
    if (!is_single() && !is_admin()) {
        $the place .= " AND post_password = ''";
    }
    return $the place;
}
add_filter( 'posts_where', 'wpb_password_post_filter' );

This code merely modifies the question despatched to the WordPress through the use of the posts_where filter. It asks WordPress to fetch all posts that shouldn’t have a password.

Go to your web site and you will notice that password protected posts are now not seen on homepage, archives, or in widgets like latest posts.

Before and after hiding protected posts in WordPress

You’ll be able to nonetheless go to the submit by accessing it by means of a direct URL to the submit itself.

The instance above, hides password protected posts from all customers. What in the event you ran a multi-author WordPress website and needed protected-posts to be viewable by customers with the potential to edit personal posts?

Merely modify the above code with one other conditional tag, like this:

1
2
3
4
5
6
7
operate wpb_password_post_filter( $the place = '' ) {
   if (!is_single() && !current_user_can('edit_private_posts') && !is_admin()) {
        $the place .= " AND post_password = ''";
    }
    return $the place;
}
add_filter( 'posts_where', 'wpb_password_post_filter' );

On this instance, we examine if a person can not edit the password protected posts, then solely present the posts that don’t have password. Doing so all customers with person roles of administrator and editor will see the password protected posts on the entrance finish of your website.

We hope this text helped you disguise password protected posts from WordPress loop in your website. You may additionally wish to see our tutorial on easy methods to change private and protected posts prefix in WordPress.

Download The WPSaviour App Now

Related posts
Beginner’s GuideBlog

How to Password Protect a Page or Post in WordPress

Beginner’s GuideBlog

How to Make Your WordPress Blog Completely Private

Beginner’s GuideBlog

How to Password Protect a Page or Post in WordPress

Leave a Reply

Your email address will not be published.

Pay in your preferred currency
Indian rupee
Euro