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 $content;
add_filter('the_excerpt_rss', 'removeRssPrivatePost');
add_filter('the_content_feed', 'removeRssPrivatePost');
  • Are you using a pre_get_posts filter by any chance? – Tom J Nowell Mar 4 at 12:15

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
} );
| improve this answer | |

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' ) );
| improve this answer | |

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.