I’m trying to extend an RSS feed to output some post-meta from my website.
I have an ‘event_date’ meta-key and I need to order by this as opposed to the RSS standard publish date, which I know how to do if I could get that information.
I’m using the following code, which makes use of the WordPress action hooks available in the RSS feeds. However, when I use these hooks, the feeds report that there are no items found, while without these the items are found, but of course I cannot order them as I need.
Am I doing something wrong in the way that I am outputting to the RSS feed?
/**
* Adds the 'event_date' meta value to a feed
*/
add_action('atom_entry', 'add_event_date_to_feed');
add_action('rdf_item', 'add_event_date_to_feed');
add_action('rss_item', 'add_event_date_to_feed');
add_action('rss2_item', 'add_event_date_to_feed');
function add_event_date_to_feed(){
global $post;
$event_date_raw = get_post_meta($post->ID, 'event_date', true);
if($event_date_raw && $event_date_raw !== '') :
$date_object = DateTime::createFromFormat('D, d M Y H:i:s +0000', $event_date_raw);
$event_date = $date->format('U');
else :
$event_date = '';
endif;
printf("tt".'<eventDate>%1$s</eventDate>'."n", $event_date);
}
WordPress uses SimplePie for reading feeds, not for generating feeds. You’re looking at two different things here:
The first part is pretty easy. WordPress supports custom RSS feed templates. You can actually replace the standard RSS feed with a custom one that includes your custom post meta.
As for consuming the custom data, that is pretty straight-forward. SimplePie will read the RSS feed and create a feed object that includes everything that was in the XML file. Just work with it like you would any other object.