Post Pic

How to: Control when your posts are available via rss

Did you ever published an article, and immediately notice an error? Sure, you can edit it, but there’s another problem: The article has already been published on your rss feed. To avoid that kind of problem, here’s a recipe to create a delay between post publication and availability throught rss feeds.

To achieve this recipe, simply paste the following code into your theme's function.php file. If your theme doesn't have this file, just create it.

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 = '5'; // 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');

The above code will add a 5 minutes delay between your post being published on your blog, and on your rss feed. To change the delay, change the value of the $wait variable, on line 9.

Credits goes to WpEngineer for this awesome recipe!

8 Responses

Dec 30 2008 16:30

I always double check my articles for typos and such. Anyway, overall it might be a useful trick. Cheers.

Dec 30 2008 20:13

i just added this code to my site last week. i commented on the original author’s site that he’s got a typo in the code which you have duplicated here, but it will still run correctly despite pulish=>publish

Dec 30 2008 21:12

@salzano: That’s right! Happilly the code still works, even with the typo :)

Jan 04 2009 04:10

Hey Jean-Baptiste, thanks for publishing our recipe, even though there was a typo ;)

Jan 13 2009 08:39

Thanks for this recipe. Is there a way of making a particular post not available in RSS?

Jan 13 2009 08:50

@Steve: Yes, you should put the posts you don’t want to be available via rss in a specific category and then exclude this category from the rss feed.
There’s other ways to do that though, I should write a recipe about it :)

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required
Blog And Make Cash