Family Encyclopedia >> Electronics

How to hide WordPress Loop password protected posts

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 still displays the post title with the prefix 'Protected'. In this article, we will show you how to hide password protected posts from 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.

How to hide WordPress Loop password protected posts

This post title can be seen on the homepage, archives, recent posts widget, etc. If you want to keep content completely private, this is not ideal.

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

With that said, let's take a look at how to hide password protected posts from the WordPress loop so other users can't see them.

Hide password protected posts in WordPress

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

 función wpb_password_post_filter ($ where = ") if (! is_single () &&! is_admin ()) $ where. =" AND post_password = ""; devuelve $ donde; add_filter ('posts_where', 'wpb_password_post_filter'); 

This code simply modifies the query sent to WordPress using the posts_where filter. It asks WordPress to find all posts that don't have a password.

Visit your website and you'll see that password-protected posts are no longer visible on the home page, archives, or in widgets like recent posts.

How to hide WordPress Loop password protected posts

You can still visit the post by accessing it via 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 to make protected posts visible to users with the ability to edit private posts?

Just modify the code above 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 = ""; devuelve $ donde; add_filter ('posts_where', 'wpb_password_post_filter'); 

In this example, we check to see if a user can't edit password-protected posts, then only show posts that don't have a password. By doing so, all users with admin and editor roles will see password-protected posts on the front end of your site.

We hope this article helped you to hide password protected posts from WordPress loop on your site. You can also check out our tutorial on how to change the prefix of private and protected posts in WordPress.

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