Why does Simplepie return feed items in a wrong order?

I have this feed from picasa (correct but arbitrary, desired order).

The result is messed up

Also, with this feed, for example.

It’s fine in regular, sorted by date feeds, but not these. In the object I get from Simplepie it has a messed up order. I don’t do anything fancy just loop over the results and display them in the found order using my commercial gallery plugin.

$rss_items = $rss->get_items(0, $rss->get_item_quantity($limit));

I have tried this (hooked into wp_feed_options) but it doesn’t change anything:
$rss->enable_order_by_date(false);

Also, when I do var_dump($rss_items); I get an array that is represented by 546503 lines of text. I don’t believe that’s normal, maybe it also hogs the memory, but I can’t even look through that data manually to see if the order is inherently bad or just gets mixed up somewhere.

Also I can’t tell if it’s Simplepie’s or WordPress’ wrapper’s fault.

4 Answers
4

At the end of class-simplepie.php there is this line:

usort($items, array(get_class($urls[0]), 'sort_items'));

Which force-sorts feed elements, in case of multifeed.

The $feed->enable_order_by_date(false); is indeed very important and the key to things, BUT it gets disregarded (what I was experiencing) if I use multifeed. Source: in get_items() of class-simplepie.php there is this:

// If we want to order it by date, check if all items have a date, and then sort it
if ($this->order_by_date && empty($this->multifeed_objects))

I used multifeed accidentally for single URLs too. Remember it’s a plugin where I didn’t know if it was going to be single or multiple URLs so it seemed convenient to always pass an array to fetch_feed().

So it now looks something like this:

$rss_url = explode(',',$rss_url); // Accept multiple URLs

if(count($rss_url) == 1){ // New code
    $rss_url = $rss_url[0];
}

add_action('wp_feed_options', array($this, 'jig_add_force_rss'), 10,2);
$rss = fetch_feed($rss_url);
remove_action('wp_feed_options', array($this, 'jig_add_force_rss'), 10);

And the action is:

function jig_add_force_rss($feed,$url){
    $feed->force_feed(true);
    $feed->enable_order_by_date(false);
}

Your solutions were working becuase you passed the RSS url as a string and not as a one-element array. I compared them with how I was using the methods and this was the difference. Now it works with and without query string in the Picasa feed URL, always same as how it looks in the browser.

Leave a Comment