Exclude Post from WordPress Feed

In a previous post I provide a solution on how to exclude certain categories from your feed. You can do the same with other content and I will show you in this post how you can exclude certain posts from your feed.

Therefore I use the filter posts_where (wp-includes/query.php), which is responsible for the output of the query. Please be aware that you should only filter, where you want to filter; In our example it is in our feed $wp_query->is_feed. If you won’t do it that way, the filter would also run in your backend and these posts won’t be displayed on post overviews.

The function has two parameters and you give the first parameter $where an extension of the SQL-string, which will take care of the filtering based on the ID. Within the brackets you have to put in the IDs of the posts, which you like to filter.

function fb_post_exclude($where, $wp_query = NULL) {
	global $wpdb;
	
	if ( !$wp_query )
		global $wp_query;
	
	if ($wp_query->is_feed) {
		// exclude post with id 40 and 9
		$where .= " AND $wpdb->posts.ID NOT IN (40, 9)";
	}
	
	return $where;
}
add_filter( 'posts_where','fb_post_exclude', 1, 2 );

Have fun with testing and adjustments of the code. The function is pretty small but it nicely shows the possibilities and you can extend it to your liking.

Update: please use for this requirement the follow solution and read the comments for this solution without SQL statement; use the filter pre_get_posts.

function fb_exclude_filter($query) {
    if ( !$query->is_admin && $query->is_feed) {
        $query->set('post__not_in', array(40, 9) ); // id of page or post
    }
    return $query;
}
add_filter( 'pre_get_posts', 'fb_exclude_filter' );

Posted

in

by

Comments

11 responses to “Exclude Post from WordPress Feed”

  1. chris Avatar
    chris

    how about adding an array so you can assign the ids to the function rather than doing it manually in the function?

  2. Andrew Nacin Avatar

    I would not do it this way. Please avoid SQL where possible. 🙁

    Instead, set the query variable post__not_in on a hook, such as pre_get_posts, when is_feed.

  3. Frank Avatar

    @Andrew: yes, you have right – ist easier for update and stable solution.
    maybe this code is an solution:

    function fb_exclude_filter($query) {
    
        if ( !$query->is_admin && $query->is_feed) {
            $query->set('post__not_in', array(40, 9) ); // id of page or post
        }
    
        return $query;
    }
    add_filter( 'pre_get_posts', 'fb_exclude_filter' );
    

    Again a thank you for diskussion and read aour blog. The best thing about WordPress exchange of knowledge.

  4. […] at WPEngineer put together a post explaining how to exclude an individual post from the WordPress RSS […]

  5. Lucian Avatar

    It is great when the code is shared and it is so easy to do something without adding a plugin that will make the website to respond slower.

  6. […] piece this morning from one of my favourite WP websites on excluding posts from your RSS feed.https://wpengineer.com/2175/exclude-post-from-wordpress-feed/About ChristopherI'm a WordPress consultant and web designer living on Canada's beautiful East […]

  7. Christopher Ross Avatar

    Frank, that’s a great piece of code to help clean up the RSS. Thanks.

  8. Greg Winiarski Avatar

    The second solution seems much better, maybe it’s worth moving it from comments to the post?

  9. […] Exclude Post from WordPress Feed (WPengineer.com) […]

  10. […] Und wen überrascht es, auch hier gibt es zwei Vorgehensweisen: mit und ohne Plugin. Die Code-Lösung fand ich auf WP-Engineer: […]