Envira Gallery Blog

Digital Photography Tips, Tutorials and Resources

How to Hide Password Protected Galleries from WordPress Loop

How to Hide Password Protected Galleries from WordPress Loop

by Waseem Abbas on Jul 29, 2016

Do you want to hide password protected galleries on your WordPress site? You may have a password protected photo gallery that you created for a client, and you don’t want it to show up in the loop along with all of your other galleries and blog posts. In this article, we will share how to hide password protected galleries from the WordPress loop.

You may have a password protected post with an image gallery in it, or you’ve created a password protected image gallery in WordPress and placed it in a blog post so that it can be viewed by your client. Only those who have the password will be able to actually access your gallery and view the photos.

However, anyone browsing your blog feed or archives will be able to see that a password protected gallery exists in the archives, recent posts, and other places that display the blog post loop.

In other words, they will be able to click through to a page that looks something like this:

Protected Prefix

This is OK for your client to see, but it looks very unprofessional on the public pages of your website. If you want your gallery to be completely invisible to the public (except for those who have the direct link to it), then you’ll need to hide the password protected post that contains your gallery from the WordPress loop.

Hide Password Protected Posts in WordPress

To completely hide password protected posts with image galleries, you should add the following code in your theme’s function.php file:


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

That’s it! Now any posts that are password protected will not show up in the loop. Now your recent posts and archives should look something like this (before and after):

Before After Hide Posts

Bonus tip: If you want to display the password protected post to users with rights to edit this post (such as your assistant), then simply remove the above code and add this code:


function eg_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', 'eg_password_post_filter' );

This code will display the password protected posts with image galleries to the user roles of administrator and editor in your WordPress site.

We hope this article has helped you to learn how to hide password protected galleries from the WordPress loop. You may also want to check out our guide on how to create a photography client login with WordPress.

If you like this article, then please follow us on Facebook and Twitter for more free WordPress tutorials.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

[i]
[i]
[i]
[i]