Drupal’s core Aggregator module is great. It’ll handle pretty much any type of feed you throw at it, store the contents locally, and let you display it all however you want.
I recently began using it at work to display some partner feeds on latina.com, but ran in to a small problem – the feed items were not being ordered by their associated timestamps. Even worse, they looked like they weren’t updating at all. This problem didn’t occur when working just with the Aggregator module, but when I used the Views module to display the feed items. Output from the Aggregator module alone worked as expected.
It turns out that, for one reason or another, Drupal was not importing the feed item’s timestamps along with everything else. It was using the time the feed was imported at instead. That doesn’t really help when you want to display the 5 newest feed items.
Views allows for sorting, but there are no fields available to sort by that would solve the problem. There is however an IID number assigned to each feed item as it’s inserted in to the database which could be used, if it was accessible by Views.
Solution
The solution was to modify the View with a preprocess function that altered the SQL query used in order to sort the results properly. The function swaps out “aggregator_item_timestamp” with “aggregator_item_iid”. All you need to do is insert the function below in to your template.php file after making a few small changes:
- Go to your View’s edit page and make sure that it’s being sorted by timestamp in descending order.
- Click the “live preview” button and copy the SQL query that is generated.
- Replace “VIEW_NAME_HERE” with your View’s name and “SQL_QUERY_HERE” with the copied SQL Query (after replacing “aggregator_item_timestamp” with “aggregator_item_iid”).
function views_views_pre_execute(&$view) {
if($view->name=="VIEW_NAME_HERE") {
$view->build_info['query']="SQL_QUERY_HERE";
}
}
Copy the template.php file back in to your theme’s folder, rebuild the theme registry, and you’re all set! You could also build this as a module so that it isn’t limited to only the theme that is currently in use. Simply create a module as you normally would and change the function name to MODULE_NAME_views_pre_execute.















