I've been trying to extend SimplePie to sort a feed in reverse order. The second feed I've distinguished with the variable $events is a feed of events I'd like to display with "oldest" date first, since that event is closest to taking place.
I've been using SimplePie's doc Simple Pie Custom Sort to create these lines of code to achieve this,
class SimplePie_Custom_Sort extends SimplePie {
function sort_items($a, $b) {
return $a->get_date('U') >= $b->get_date('U');
}
}
however it doesn't appear to be working. I've even copied and pasted the code directly from the doc above to attempt sorting the feed by string length, and that doesn't work either.
If I go directly into the simplepie.inc file and edit the sort function there I can get both feeds to display in reverse order. All these reasons lead me to believe there is either an error in the way I'm attempting to extend the function using Simple_Pie_Custom_Sort. Or maybe I'm trying to do something that SimplePie is able to do.
I've gone as far as to upload a new copy of the simplepie.inc file, wondering if I accidentally saved over it at some point.
I feel like I probably have a silly error somewhere. I'd appreciate any someone can offer. The whole code block I'm working with is below. Thanks.
<?php
//TURN OF ERROR REPORTING
//error_reporting(0);
//INCLUDE SIMPLEPIE (PULLS RSS FEED)
require_once("inc/simplepie.inc");
// Extend the SimplePie class and override the existing sort_items() function with our own.
class SimplePie_Custom_Sort extends SimplePie {
function sort_items($a, $b)
{
return $a->get_date('U') >= $b->get_date('U');
}
}
//NEW SIMPLEPIE
$news = new SimplePie();
//SET LOCATIONS OF FEEDS
$news->set_feed_url(array(
'http://rss-feed-url-example.rss'
));
//SET UP CACHING
$news->enable_cache(false);
//$news->set_cache_location('cache');
//$news->set_cache_duration(1800);
//START THE PROCESS
$news->init();
//HANDLE FEED TYPE
$news->handle_content_type();
//NEW SIMPLEPIE USING CUSTOM SORTING
$events = new SimplePie_Custom_Sort();
//SET LOCATIONS OF FEEDS
$events->set_feed_url(array(
'http://rss-feed-url-example.rss'
));
//ANOTHER POSSIBLE NEED FOR SORTING RSS DIFFERENTLY
$events->enable_order_by_date(false);
//SET UP CACHING
$events->enable_cache(false);
//$events->set_cache_location('cache');
//$events->set_cache_duration(1800);
//START THE PROCESS
$events->init();
//HANDLE FEED TYPE
$events->handle_content_type();
?>
I figured out what the problem was. SimplePie didn't like that I had the single feed in an array.
I Changed this...
//SET LOCATIONS OF FEEDS
$events->set_feed_url(array(
'http://rss-feed-url-example.rss'
));
To this...
//SET LOCATIONS OF FEEDS
$events->set_feed_url('http://www.coloradotechnology.org/resource/rss/events.rss');
If anyone can elaborate on why this was causing issues, I'd love to hear a better explanation.