How can I exclude certain posts from WordPress RSS feeds?

Is there a way to exclude a post from all of WordPress’ RSS feeds (the default one as well as tags, categories, and search feeds)?

Related posts

Leave a Reply

1 comment

  1. Assuming you do not want to use plugins like THIS ONE or THIS, you have many ways :

    One is to assign a category for the posts you would like to exclude and then you can just change the URL of the RSS link like so :

    http://www.mydomain.com/feed?cat=-x,-y,-z
    

    You can also FILTER with a function :

    function o99_my_rss_filter($query) {
    if ($query->is_feed) {
    $query->set('cat','-7'); //Put category ID - here it is : 7
    }
    return $query;
    }
    
    add_filter('pre_get_posts','o99_my_rss_filter');
    

    The point is that you will need to assign SOMETHING (tag, custom field, category) to identify what to exclude , assuming that you do not want always to add the ID to exclude from the query.

    The easiest is by category as demonstrated above .

    ** Edit I **

    Just for the sake of it – (I still recommend the cetegory method) r
    Even if I dislike custom-fields when they are overused – this is another way that change the query by assigning a custom field :

    function o99_my_rss_filter_by _field( $where, $wp_query = NULL ) {
        global $wpdb;
    
        if ( !$wp_query ) global $wp_query;if ( $wp_query->is_feed ) {
    
            $posts = get_posts( array( 'meta_key' => 'norss' ) );
    
            if ( $posts ) {
                foreach( $posts as $post ) {
                    $exclude .= $post->ID . ',';
                }
            }
            $exclude = substr( $exclude,0, strlen( $exclude )-1 );
    
            $where .= ' AND $wpdb->posts.ID NOT IN ( ' . $exclude . ')';
        }
        return $where;
    }
    add_filter( 'posts_where', 'o99_my_rss_filter_by _field', 1, 4 );