If you work with RSS feeds or you provide one for your blog, at one time or another you might have encountered this issue.

Once you publish a post, it’s in your RSS feed, and if you notice any mistakes in this post right after you’ve published it (and it happens) there is no way to go back and change it within the RSS feed.

So, while you can modify the post itself and make the necessary fixes, your RSS feed will still be providing the original version, containing any mistakes you might have made.

This can be very frustrating, especially when you know that your RSS feeds are used by many of your followers. None of us like having mistakes in our work, let alone having them pointed out to us over and over simply because the fixes we made weren’t updated in the RSS feed.

The Fix

There’s a way around this, and all it takes is delaying the post from being added to your RSS feed. In other words, rather than having the post automatically added to your feed as soon as it’s published, it will be added a few minutes, hours or even days later – it’s all up to you.

All it takes is adding a few lines of code to your WordPress theme’s functions.php file. If you don’t have one, just create it and add the following code in the same way.

So here’s the code you’ll need. I’ll explain how it works a bit further down.

function publish_later_on_feed($where) {
    global $wpdb;

    if ( is_feed() ) {
        // timestamp in WP-format
        $now = gmdate('Y-m-d H:i:s');

        // value for wait; + device
        $wait = '10'; // integer

        // http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
        $device = 'MINUTE'; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR

        // add SQL-sytax to default $where
        $where .= " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$now') > $wait ";
    return $where;

add_filter('posts_where', 'publish_later_on_feed');

How It Works

Well, this filter basically adds a 10-minute delay between the post being published on your WordPress site and it appearing in your RSS feed. This gives you the chance to go back and fix any mistakes before it’s permanently present in the feed.

If you want the delay to be longer or shorter than 10 minutes, all you have to do is change the value of the $wait variable on line 9 to whatever value you want it to be.

In case you want the delay to be longer than just a few minutes, you can also change the value of the $where variable to hours, days, weeks, months or even years (but be sensible, you always want your content to be seen as soon as it’s relevant).

Original Source: WP Engineer

Published by Mark Zahra

Project Manager - WP RSS Aggregator

Leave a Reply

Your email address will not be published. Required fields are marked *