Sort entities by recently commented

Is it possible to sort entities by most recently commented?

  • Short answer: yes.

    Maybe a bit more details... For example Tidypics has a page that lists photos in order of most recent comments. The code (in mod/tidypics/views/default/resources/tidypics/lists/recentlycommented.php) used to get the entities in the corresponding order is

    $db_prefix = elgg_get_config('dbprefix');
    $options = array(
        'type' => 'object',
        'subtype' => 'image',
        'limit' => $limit,
        'offset' => $offset,
        'joins' => array(
            "JOIN {$db_prefix}entities ce ON ce.container_guid = e.guid",
            "JOIN {$db_prefix}entity_subtypes cs ON ce.subtype = AND cs.subtype = 'comment'"),
        'order_by' => "ce.time_created DESC",
        'full_view' => false,
        'list_type' => 'gallery',
        'gallery_class' => 'tidypics-gallery'
    $result = elgg_list_entities($options);

    Of course, you would have to modify it a bit for other type of entities and implement it within a view that outputs it. For further reference you could take a look at the code of the Tidypics plugin.