How to get the last updated date of a post from a WP RSS feed?

I have a third party tool which extracts feed content from our WordPress site, via our RSS feed: http://ontariotravelblog.com/feed

The feed provides me with everything I need, except for the last updated date of the blog post; it does include the published date, however this is simply the date the blog was first published; edits / changes made to the blog after publication does not affect this date.

Now what’s interesting is that the ATOM version of the feed (http://ontariotravelblog.com/feed/atom) does include both the published date and the updated date, for example:

 <entry>
  ...
    <title type="html">
        <![CDATA[8 Songs for Your Ontario Summer Road Trip Playlist]]>
    </title>
    <id>http://ontariotravelblog.com/?p=5621</id>
    <updated>2016-06-09T16:40:46Z</updated>
    <published>2016-05-10T16:25:35Z</published>
 ...
 </entry>

Which is great, except that the ATOM feed is missing some critical content (in particular, its missing a number of “media:” elements, such as media:thumbnail, etc.)

So after all this, my question: Is there a way to get the updated field which is included within the ATOM feed entry to appear on the main RSS feed? (perhaps via some config option?)

1 Answer
1

Atom vs RSS2

Let’s look at the wp-includes/feed-atom.php and wp-includes/feed-rss2.php files.

The updated element of the Atom feed entry is:

<updated><?php 
    echo mysql2date(
        'Y-m-d\TH:i:s\Z', 
        get_lastpostmodified('GMT'), 
        false 
    ); 
?></updated>

The pubDate element of the RSS2 feed item is:

<pubDate><?php 
    echo mysql2date(
        'D, d M Y H:i:s +0000', 
        get_post_time('Y-m-d H:i:s', true), 
        false
    ); 
?></pubDate>

The lastBuildDate element of the RSS2 channel is:

<lastBuildDate><?php 
    echo mysql2date(
        'D, d M Y H:i:s +0000', 
        get_lastpostmodified('GMT'), 
        false
    ); 
?></lastBuildDate>

Namespace

The RSS2 feed already contains the Atom namespace:

xmlns:atom="http://www.w3.org/2005/Atom"

so I think we can use the <atom:updated> element for our custom updated element. You might want to check it out further, if that fulfills the standard or if there are other possible namespaces suitable for this.

For the latter case we can use the rss2_ns action to add the relevant namespace.

Inject a custom element

We can use the rss2_item action to inject custom item elements, like:

add_action( 'rss2_item', function()
{   
    printf( 
        '<atom:updated>%s</atom:updated>',
         get_post_modified_time( 'D, d M Y H:i:s +0000', true )
    );

} );

Hopefully you can adjust it to your needs.

Leave a Comment