WordPress’s lastBuildDate in RSS Feed – gotcha’s

It has been ages since I wrote some technical stuff over here.

I recently stumbled into what apparently is a long-standing issue in WordPress, to the point that a lot of people decide to just implement their own, separate, RSS feed (there are a lot of articles out there on how to do that but I didn’t like such a solution).
The issue is that it doesn’t advance the ‘lastBuildDate’ field in the field often enough and we also didn’t like the logic that it uses to calculate it. In fact, by default, WordPress has logic to pull the last ‘modified’ date rather than the ‘published’ date of a post, then it also includes dates from comments… not what we needed since I am working for a news website where publication is almost always scheduled (which means the ‘modified’ date could be even a day older than the ‘publish’ date) and where comments are disabled!

More importantly, this was affecting how often Google actually processed their feed – not fast enough because it thought it was stale/not updated – and this severely impacted their chance to be found in Google News.

Here’s my solution (code to add to your functions.php or a custom plugin):

/* speed up feed cache */
add_filter( 'wp_feed_cache_transient_lifetime', create_function('$a', 'return 600;') );

/* overrides/changes the way 'lastBuildDate' is calculated for the standard wordpress RSS feed */
function my_lastpostmodified()
{
    global $wpdb;
    $add_seconds_server = date('Z');
    $lastDateSql = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
    $lastDate = date_create($lastDateSql);
    return $lastDate->format('r');
}

add_filter('get_lastpostmodified', 'my_lastpostmodified');
add_filter('get_feed_build_date', 'my_lastpostmodified');

Sources that helped me come up with this solution:

Ca(p)tching Cats and Dogs

I read on Jeff Atwood’s blog about most strong Captcha having been defeated. Also, on top of visitors getting annoyed by it, the Captcha plugin I am using has gone unmantained lately. And, one way or another, I am getting comment spam again. Which is something I really hate as you know what I would love to do to spammers…

I am seriously considering giving Asirra a try. It is an interesting project from Microsoft Research for an HIP (Human Interaction Proof) that uses info from petfinder.com to let users set apart pictures of dogs from those of cats. There is also a WordPress plugin, in the best and newest “we want to interoperate” fashion that we are finally getting at Microsoft (this has always been the way to go, IMHO, and BTW).

Anyway, what do you think ?

On this website we use first or third-party tools that store small files (cookie) on your device. Cookies are normally used to allow the site to run properly (technical cookies), to generate navigation usage reports (statistics cookies) and to suitable advertise our services/products (profiling cookies). We can directly use technical cookies, but you have the right to choose whether or not to enable statistical and profiling cookies. Enabling these cookies, you help us to offer you a better experience. Cookie and Privacy policy