Saturday 26 January 2013

Exclude post from wordpress feed

How to exclude post from wordpress feed?

Some of your posts, which you don't want to add in feed, here is the solution for that.
Here I use the filter posts_where(wp-includes/query.php), which is responsible for the output of the query.

Make sure you use the filter where you want to filter. Here it is in our feed $wp_query -> is _feed.
Otherwise it may be hide your post in post overviews.

In this function there is two parameters. In first parameter $where an extension of the my-SQL string, use for the filter of post based on ID. In bracket you have to put the posts ID which you want 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 );

You can easily edit it with your requirements, function is so simple & easy to edit.

No comments:

Post a Comment