Go to the top

Exclude Password-Protected Posts from Recent Posts Feed

Sometimes we use our WordPress “posts” for things like client proposals (ok, we should really be creating a custom-post-type for that, but we don’t always) which we password protect in order to keep the general public out of there. Of course, one of the problems with this is that a link to this Post/Proposal will appear in your “Recent Posts” feed. It’s easy to exclude these password-protected posts though, with just a little bit of code in the functions.php of your active child theme.

// Create a new filtering function that will add our where clause to the query
function my_password_post_filter( $where = '' ) {
    // Make sure this only applies to loops / feeds on the frontend
    if (!is_single() && !is_admin()) {
        // exclude password protected
        $where .= " AND post_password = ''";
    return $where;
add_filter( 'posts_where', 'my_password_post_filter' );

Quick…easy…worth the few extra lines of code, I think. Let me know if you’ve used this on any of your sites, or if you’ve accomplished the same thing another way.