0

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
    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
    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
    May 5, 2021 at 13:14

1 Answer 1

2

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, privacy policy and cookie policy

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