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:

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