1

I want to remove all password protected posts from the default RSS feed. Unfortunately I was only able to find one code example, but it looks like something is missing.

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' );

As soon as I save this function the RSS feed displays all post types (attachments, etc.) that are registered to the system instead of only published posts.

If possible I would like to solve this without an additional plugin.

Thanks

3
  • I don't believe you need to use posts_where, you're given the query object in the filter so just tell it you don't want passworded posts using $query->set(...
    – Tom J Nowell
    Commented May 5, 2021 at 12:24
  • Thank you, Tom! $query->set( 'has_password', false ); did the job. I didn't knew that was possible.
    – eistruhe
    Commented May 5, 2021 at 12:49
  • can you post that as a solution below? I can't upvote and give you points for comments :(
    – Tom J Nowell
    Commented May 5, 2021 at 13:14

1 Answer 1

3

Here's the filter to exclude password protected posts from the default RSS feed:

function rss_filter_protected( $query ) {
    if ( $query->is_feed ) {
        $query->set( 'has_password', false );
    }
    return $query;
}
add_filter( 'pre_get_posts','rss_filter_protected' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.