0

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();

?>
  • What are you getting, error messages, also what version of PHP are you running? If you are getting Deprecated reference errors, you can fix that by removing all the =& and replacing them with = in simplepie.inc. – Ryan Jun 14 '12 at 23:40
  • PHP Version 5.2.17. I'm not getting any errors, that is the weird part. From as far as I can tell it should be working. I'll try to replace the =&. I commented on Facebook. Guess I should of commented here. – Jarred Dotterer Jun 19 '12 at 17:33
  • I just ran the comparability test SimplePie comes with. Everything checked out there. – Jarred Dotterer Jun 19 '12 at 17:51

2 Answers2

0

You have

$events->enable_order_by_date(false);

This completely disables sorting, causing your items not to be sorted. (It's a badly named method.)

Ryan McCue
  • 1,628
  • 14
  • 23
  • Ryan, Thanks for the advice. I deleted that line of code, and I'm still not getting the result I'm looking for. I do believe you are correct and that line was causing me grief. I'm going to keep working at figuring out the problem. – Jarred Dotterer Jun 15 '12 at 15:51
0

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.

  • Perhaps internally SimplePie creates a multi-feed instance because you passed an array. And then things don't work well. – hakre Jun 19 '12 at 19:53