Why does Simplepie return feed items in a wrong order?

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