Publish The Feed Later
Some of you know this problem already, you publish a post and while you are publishing you notice an error. That’s very annoying, the error is in your feed an published to the whole world.
That happened quite often to me, my fellow readers a writing comments so fast, right after publishing, to tell me about some errors I made, it’s unbelievable. So I thought I should create a workaround to have the feed published a little bit later, even 5 minutes can be helpful. Here we go:
WordPress works with a special query, which acts differently depending on the user rights. It is always saved in the variable $where
and differently assembled. That means, you can extend it.
If you look at the possibilities of mySQL, there is a function timestampdiff()
. I extend the query of WordPress with this function. Note to query the feed before (is_feed()
), if not it will be also happening for the classical publishing of posts in your blog.
The following example publishs the post 5 minutes later in your feed:
/**
* puplish the content in the feed later
* $where ist default-var in WordPress (wp-includes/query.php)
* This function an a SQL-syntax
*/
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');
If you put the above syntax in your functions.php
of your theme, then your feed will be published always 5 minutes later. Of course you can adjust the time, also the units, if you need it in a special case. I probably will add this feature in the next release of my Plugin ©Feed.
Thanks for this tip.
But i dont understand, why u want that publish later?
Hey Garaj,
the reason why you might want to publish later is pretty simple. When you publish your feed, people will get your post with all errors immediately. It takes time before the feed updates, if you update your post, it will show the changes immediately, but your feed will stay the same until the next update, an update period can take hours, which is annoying, if all your readers can read the errors in your feed, even if you had correct it some hours ago. Hope that makes it clear. 🙂
Ola there
Off topic: could anyone from this site contact me? (I couldn’t find any “Contact” form or link here). I’d like to add your site to http://planetwordpress.planetozh.com/
Thanks in advance
Hey Ozh,
I just sent you an email, glad to be part of your planetozh 🙂
Thanks a lot, can’t be a better compliment!
Thanks for the tip/code. Always find myself finding mistakes minutes after I publish an article.
Any way to edit a feed once it is already published?
You are welcome Christian
Hey there Alex sounds like the right hack for me! Though when pasting it like this into my function.php I get errors in my wp-admin and a white stripe on top of my blog design. What could cause that?
Hey Simon, can you tell me what exactly does it say in your error.
Thanks
Actually, pasting exactly the same as above into the functions.php I get this on top when I want to log in to the admin back.
/** * puplish the content in the feed later * $where ist default-var in WordPress (wp-includes/query.php) * This function an a SQL-syntax */ function pulish_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', 'pulish_later_on_feed');
Simon, you have to use php tags before and after the code like that:
<?php
//here comes the code
?>
Grand Michael I tried that yesterday, good to have the confirmation. Thanks a lot!
Marvelous hack!
I can’t count how many times I accidentally broadcast unfinished post.
Thanks!
You are welcome Rupert 🙂
Okay, I tried to add the code at the end of my functions php and this is what I get when I want to go to my admin:
Fatal error: Call to undefined function add_filter() in /…/…/public_html/wp-includes/functions.php on line 2928
can you help?
Thanks for posting, is exactly what I need!
yemoonyah, you have to use the functions.php in your theme folder, not the wp-includes/functions.php.
If you don’t have a functions.php in your theme folder, create one.
OK, I think this mechanism is very close to something I’m looking for. The problem is that I don’t know PHP.
What I would like to see is the following:
An algorithm, that, given a specific category, would delay the publishing of a post.
So, for instance: if the category is “Sports”, the delay would be one day. If the category is “Hot News”, the delay would be zero.
Is there anyone around here who could write up something for me? Thanks in advance.
I’ll send you something from the Netherlands…
Sjakelien: Yes this is also possible. PLease check this with the conditional tags:
if ( is_feed() && is_category('6') ) {
OK,thanks, but where would I put that, and what would the publication date/time parameter be?
awesome! this is exactly what i need. small typo in your function name, btw…
pulish => publish
Thanks salzano! Fixed.
Sjakelien, in the same function.
function publish_later_on_feed($where) {
...
if ( is_feed() && is_category('6') ) {
...
Is it possible I’ve got some settings wrong here? I don’t get any feedburner mails/feed anymore since adding this (exact copy) to my functions.php?
Thank you a lot!
I found that when I pasted this into the functions.php file it would cause my RSS feed to no long work. If you went to the URL of the feed there would be a page not found error and feedburner could no longer use the feed.
I found that once this was code was deleted from the functions.php file everything worked again. I liked the concept of this but if using it is going to cause my feed to be unavailable I will deal with publishing the RSS at the same time.
Have you encountered any one else with this problem?
Yeah Erik, same thing here although the feedburner feed is still there, just with all the items up to when I added the code to the functions.php. Any recommendations?
The code works fine and i think you have only a problem in your theme or a problem with other plugins. The function wait 10 minutes and the is the same content in the feed. Feedburner scan the code in different interval with and publish later. Maybe is this a problem with this function.
I tried it with the default WordPress theme and it was still causing the same problem. I turned off all of my plug ins and there was still the same problem. It had nothing to do with Feedburner this code caused the feed from WordPress (url.com/feed) to no longer work.
I have tested the code under many blogs and i use the code in many live blogs. Do you have correct copy the code in the functions.php of the Theme, with the php-tags?
Hey dude.. can you help me?
My server time (php date): 9:13 AM
My server time (php gmdate): 5:13 PN
Wordpress time: 1:13 PM
How can I change your var _now to make this works with this?
thanks
fixed.. it was that ‘greater than’ character
Why Publish The Feed Later?
@AlexPTS. Answered in a previous comment.
Do I still need to apply this if I am using feedburner since you mention that is scans in intervals?
It just came to my attention that my feed seems to have a delay of 1 message.
So if I post a message, the message before that one appears in my feed. My readers read my blogposts when they are already dated.
I neutralized this script and my issue was over. I am going to switch it on later this day just before publishing a new artciel to double check if it really originates from this script.
Hey Erik, I’m having the same problem. On the site we work (WP 2.7.1) we’re getting same kind of delay.
I still have this issue and not sure what is the reason. If i post a message it is not included in the fed directly, even after pinging and a refresh (I use feedburner).
last time it worked when I changed the time of posting. I do not spend much time testing since it is a smaller blog where this happens.
Thanks very much – really appreciated!
I create this script to publish rss weekly based on this post.
Thanks for the idea!
add this script in your functions.php of your theme.
/**
* puplish the content in the feed later
* $where ist default-var in WordPress (wp-includes/query.php)
* This function an a SQL-syntax
*/
function publish_weekly_on_feed($where) {
global $wpdb;
if ( is_feed() ) {
$day=date(N);
$now = gmdate(‘Y-m-d H:i:s’);
// value for wait; + device
$wait = ‘1’; // integer
// http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
$device = ‘WEEK’; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR
$where .= ” AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, ‘$now’) > $wait “;
$where .= ” OR $day = ‘4’ “; //1 = monday, 2= tuesday …. 7 =sunday
}
return $where;
}
add_filter(‘posts_where’, ‘publish_weekly_on_feed’);
I just tossed this in my function.php but I haven’t published since doing so. We’ll see how it goes tomorrow!
Thanks!
How can I delay just one feed. I’d like my main feed to publish immediately, but I’d like to have another feed that I can provide to my syndication partners that has a delay. Any suggestions?
I would also like to know how to delay just one feed, while leaving other feeds not delayed. Or how to create a special xml page that displays the delayed version of the feed.
Our website uses wordpress as a platform. We tested our RSS and there’s a whole month delays. So the blog post i’ll get on my outlook or google reader would be the post last month rather than the latest one. Where could the problem lie?
Is it the same principle as this? I mean delaying 5 minutes. Could it be possible that rather than delaying 5 minutes, ours is delayed in 30 days?
I’ve tried your function…works great!
Now…how can I add to the function a filter that excludes posts that have already been published? Sometimes I update a post and I don’t want these updated posts going back through the feed.
I’m thinking I could use a custom field to mark the post but I just don’t know how to write the query.
Thanks
Thanks for this guide. But if i want that google index content fast i ping rss feed. One real scenarion:
– aggregator post in blog
– blog generate feed from posts
– i ping by cron job generated rss feed
Is simple – if feed is generated after 5 minutes, just content will be indexed after 5 minutes. In this way you must create 2 feeds – one public that is with 5 or better with 15 minutes delay and one private that you will use only to ping google to index .