We needed to create an RSS feed of WordPress posts that had been deleted, and it doesn’t exist out of the box, but it is very simple to create one. This information is available by searching but I thought it would be useful to have a single resource for this specific case.
We’re going to add a custom feed using WordPress’ add_feed function. Let’s add this to our functions.php file:
// Custom trashed items feed
function add_custom_trashed_posts_feed() {
add_feed('trasheditemsfeed', 'make_trashed_posts_feed');
//flush_rewrite_rules(); // need to do this once, then comment it out
}
add_action('init', 'add_custom_trashed_posts_feed');
function make_trashed_posts_feed() {
get_template_part('rss', 'trasheditemsfeed');
}
And this template file into our main WP folder called rss-trasheditemsfeed.php:
<?php
/**
* Template Name: Custom RSS Template - trasheditemsfeed
*/
$postCount = 10; // The number of posts to show in the feed
$posts = query_posts( array(
'post_status' => 'trash',
'showposts' => $postCount,
'orderby' => 'modified',
) );
header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
<?php do_action('rss2_ns'); ?>>
<channel>
<title><?php bloginfo_rss('name'); ?> - Trashed Posts Feed</title>
<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
<link><?php bloginfo_rss('url') ?></link>
<description><?php bloginfo_rss('description') ?></description>
<lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
<language><?php echo get_option('rss_language'); ?></language>
<?php do_action('rss2_head'); ?>
<?php while(have_posts()) : the_post(); ?>
<item>
<title><?php the_title_rss(); ?></title>
<link><?php the_permalink_rss(); ?></link>
<pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
<modified><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_modified_time('Y-m-d H:i:s', true), false); ?></modified>
<dc:creator><?php the_author(); ?></dc:creator>
<guid isPermaLink="false"><?php the_guid(); ?></guid>
<?php rss_enclosure(); ?>
<?php do_action('rss2_item'); ?>
</item>
<?php endwhile; ?>
</channel>
</rss>
Okay, now try mywebsite.com/feed/trasheditemsfeed and you’ll get a 404 not found. Don’t worry, uncomment this line to reset the url rewrite rules for your site so WP will see your new feed:
flush_rewrite_rules(); // need to do this once, then comment it out
Try the url again and you should see your feed. Then comment the line back out and re-save the code – you don’t want to run this command over and over.
This will now show the last 10 trashed posts.
Let’s review the code.
First we add the custom feed with an add action to init, the add_feed takes two arguments, the name of the feed (as seen in the url), and the function used to create it. In the function we simply call the template for the feed which is of the format rss-{your-feed-name}.php, and is stored in the usual template location.
In the feed template itself we use query_posts with args to say we want trashed items and to order them by modified fate – when you trash a post its modified data gets updated to we can use this to always get the most recent posts in the list. The rest of the template should be pretty simple to understand – you can add in or remove items easily. Check the default WP rss2 template in wp-includes/feed-rss2.php.
Comments