Edit WordPress Feed

Ever thought you could make some improvements to your RSS feed? Like letting it cover more (or less!) content? Or adding some extra details onto the end of your posts?

In this post, I’m going to show you how to do exactly that. Here’s what we’ll be covering:

  • How to include both posts and pages in your feeds.
  • How to add thumbnails to your feeds.
  • How to exclude posts with a certain tag.
  • Set how many posts appear in your feed (Without affecting the rest of your site).
  • How to show only posts from a certain category.
  • How to add content to the end of each post (e.g. link to your latest featured post).

All of this is going to take place in the functions.php file of your theme. If your theme doesn’t have one, just create a file in your theme’s folder with that name and let’s get to it! (Make sure all of this code goes between the opening tag in the file)

Include Pages in WordPress Feed

What we will do is add a filter to WordPress when it is searching for posts. The filter is going to check if the posts are for a feed, and if they are, it’s going to adjust the query to include both posts and pages.

function feedFilter($query) {
	if ($query->is_feed) {
		$query->set('post_type','any');
		}
	return $query;
}
add_filter('pre_get_posts','feedFilter');

If you wanted to show only pages, then you could change the word ‘any’ to ‘page’ (or to the name of any custom post types you’ve created).

You may want to be more specific and only show top level pages. In that case, you could use this code with an extra line to show only top level pages:

function feedFilter($query) {
	if ($query->is_feed) {
		$query->set('post_type','any');
		$query->set('post_parent','0');
		}
	return $query;
}
add_filter('pre_get_posts','feedFilter');

Add Thumbnails to RSS Feed

The process this time is slightly different. Again we’re going to add a filter to the query and test if the page is for an RSS feed. This time we won’t adjust the query though (We’ll return it exactly as it was), but we will add a filter to the_content (i.e. the content of your posts).

function feedFilter($query) {
	if ($query->is_feed) {
		add_filter('the_content', 'feedContentFilter');
		}
	return $query;
}
add_filter('pre_get_posts','feedFilter');
 
function feedContentFilter($content) {
	$thumbId = get_post_thumbnail_id();
 
	if($thumbId) {
		$img = wp_get_attachment_image_src($thumbId);
		$image = '<img align="left" src="'. $img[0] .'" alt="" width="'. $img[1] .'" height="'. $img[2] .'" />';
		echo $image;
	}
 
	return $content;
}

We’re using a slightly roundabout way of getting the thumbnail image so that we can add the align="left" part. A lot of feed readers strip out inline CSS, but using the old-fashioned align property should work. And of course, if you don’t want your images aligned to the left, just take out the align="left" altogether!

You can use that code to get any size of thumbnail as well, e.g. let’s say you had added this line to your functions.php file to define a special thumbnail size just for feeds:

add_image_size('feed', 600, 100, true);

Then you could adjust the 7th line from the end to read:

$img = wp_get_attachment_image_src($thumbId, 'feed');

How to Exclude Posts With a Certain Tag

This time we’re going to do something similar to the first example. We’ll be using ‘set’ to adjust the query object. If you want to read more about what you can do with querys, check out the WP_Query codex page.

The only nuisance is that we have to first work out the ID of the tag we want to exclude. To do this, go to Posts > Post Tags, then find the tag you want to exclude and click on it. In your browser’s address bar, you’ll see that part of the URL looks like this: &tag_ID=29

So in that case, 29 is the tag ID.

function feedFilter($query) {
	if ($query->is_feed) {
		$tags = array('29');
		$query->set('tag__not_in', $tags);
	}
 
	return $query;
}
add_filter('pre_get_posts','feedFilter');

If you wanted to exclude posts from multiple tags, you could list them on the 3rd line, e.g.

$tags = array('29', '31', '124');

Control How Many Posts Appear in the Feed

In your dashboard, under Settings > Reading, you can set how many posts appear on your site pages and in your feed. However, there may be times when you want to show more posts in your feed than on your site.

e.g. your site may look best with just 3 or 4 posts per page, but you’d definitely want more than that in your feed!

To make that change, just use the code below:

function feedFilter($query) {
	if ($query->is_feed) {
		$query->set('posts_per_page','20');
	}
 
	return $query;
}
add_filter('pre_get_posts','feedFilter');

Feel free to change the 20 to any number you like!

Show Posts from Only One Category

Normal blogs are unlikely to do this, but if you were using WordPress as a CMS, you may want to publish only posts from your “blog” category. In that case, you would do this:

function feedFilter($query) {
	if ($query->is_feed) {
		$query->set('category_name', 'blog');
	}
 
	return $query;
}
add_filter('pre_get_posts','feedFilter');

And again, you could turn that on its head to exclude posts from a certain category. In that case, the 3rd line would be:

$query->set('cat', '-45');

Where 45 is the ID of the category (The minus sign must be included as well, otherwise you’ll end up showing only posts from category 45!).

Add Content to the End of Each RSS Post

There are plenty of reason you might want to add some content to the end of your RSS posts. In this first example, we’ll add a simple line that says:

"Thanks for reading, check out Your Blog name for more WordPress news!"

function feedFilter($query) {
	if ($query->is_feed) {
		add_filter('the_content','feedContentFilter');
	}
	return $query;
}
add_filter('pre_get_posts','feedFilter');
 
function feedContentFilter($content) {
	$content .= '<p>Thanks for reading, check out <a href="'. get_bloginfo('url') .'">'. get_bloginfo('name') .'</a> for more WordPress news!</p>';
 
	return $content;
}

Now let’s do something slightly cooler. Let’s say that you have a featured content slider on your homepage, or a featured posts list in your sidebar, and you’ve set it up so that to add posts to either of these, you tag them with “featured.”

In this last example, we’ll add a byline to your feed posts that says:

"Make sure not to miss our latest featured post: Post Title"

In our feedContentFIlter function this time, we will run a query to get the latest post tagged with featured. We’ll then use the post object we get back to insert the post title and address.

function feedFilter($query) {
	if ($query->is_feed) {
		add_filter('the_content','feedContentFilter');
	}
	return $query;
}
add_filter('pre_get_posts','feedFilter');
 
function feedContentFilter($content) {
 
	$args = array(
	    'numberposts' => 1,
	    'tag' => 'featured'
	);
	$posts = get_posts($args);
 
	if($posts) {
	    foreach($posts as $post) {
	        $content .= '<p>Make sure not to miss our latest featured post: <a href="'. get_permalink($post->ID) .'">'. $post->post_title .'</a>!</p>';
	    }
	}
 
	return $content;
}

You could enhance that however you like, e.g. including some inline CSS to adjust the appearance of the line.

How Else Could You Edit Your Feed?

Those are some ideas for what you could do with your feed, but is there anything else you’d like to do with it?

Let me know in the comments and if there are any great ideas, I’ll show you how to do them!

Update (20th August): Some commenters have mentioned already that all of these changes are for the main feed. If you’d like to create a new feed and customize that, check out this post by WordPress Recipes for a good starting point! (You’ll need to adapt the code samples above slightly though)

Enjoy this post? You should follow me on Twitter!