admin管理员组

文章数量:1302545

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

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');
Share Improve this question edited Mar 4, 2020 at 12:51 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Mar 4, 2020 at 12:02 RafaucauRafaucau 1738 bronze badges 1
  • Are you using a pre_get_posts filter by any chance? – Tom J Nowell Commented Mar 4, 2020 at 12:15
Add a comment  | 

3 Answers 3

Reset to default 1

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

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.

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

本文标签: How to remove private posts from RSS feeds