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);
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
WordPress uses SimplePie for reading feeds, not for generating feeds. You’re looking at two different things here:
- You want to output your custom post meta in an RSS feed.
- You want to read that custom post meta somewhere else.
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0