How to remove private posts from RSS feeds?

Private posts are visible only to administrators and editors, but I have found that they are displayed publicly in RSS feeds. How can I remove them from there?

I have this code but it doesn't work:

function removeRssPrivatePost($content)
{
  global $post;
  if ($post-post_status == 'private') {
    return;
  }

  return $content;
}
add_filter('the_excerpt_rss', 'removeRssPrivatePost');
add_filter('the_content_feed', 'removeRssPrivatePost');

Topic private feed rss Wordpress

Category Web


WordPress should not display private posts in public RSS feeds, except for those privileged users you mentioned, and even they need to be viewing the feed in a browser where they've logged in to your site. If your WordPress does display private posts in the feed for everybody, there's something wrong; perhaps a misbehaving plugin or theme.


The same way you would change which posts WP shows on any other screen, pre_get_posts! If you ever see WP pull in posts, and want to modify what it fetches from the DB, use the pre_get_posts filter

E.g. something similar to this:

add_action( 'pre_get_posts', function( \WP_Query $query ) {
    if ( !$query->is_feed() ) {
        return; // this isn't a feed, abort! 
    }
    $query->set( 'post_status', 'publish' ); // we only want published posts, no drafts or private
} );

Does your theme have a 'feed-rss.php'?

You can add to the query in there so it only shows certain post status'.

$the_query = new WP_Query( array( 'post_status' => array( 'publish' ) );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.