List only latest entity of each user

How can I list only the latest entity of each user?

$options = array(
    'type' => 'object',
    'subtype' => 'file',
);

$files = elgg_list_entities($options);

  • Try this:

    
    $list_params = array(
         'limit' => 4,
         'full_view' => false,
         'list_type_toggle' => false,
         'pagination' => false,
         'owner_guid' => elgg_get_logged_in_user_guid(),
    );
    
    //grab the latest 4 blog posts for the user
    $list_params['subtype'] = 'blog';
    $blogs = elgg_list_entities($list_params);
    
    //grab the latest bookmarks for the user
    $list_params['subtype'] = 'bookmarks';
    $bookmarks = elgg_list_entities($list_params);
    
    //grab the latest files for the user
    $list_params['subtype'] = 'file';
    $files = elgg_list_entities($list_params);
  • I'm not trying to list a single owners entities of a subtype.  I want to list all entities of a subtype but only want to show the latest one entity of each user.  If someone posted 5 times to the entity "file" I just want to show the latest post not all 5 and then I want to show the rest for all users that posted to the subtype "file"

  • 'group_by' => 'e.owner_guid' might do the trick

  • Ismayil this was so close and way easier then what I was trying to do!  The only issue is its show the oldest post not the newest post.  Any ideas?

  • Maybe some finetuning is necessary, e.g. additionally to 'group_by' also

    'order_by' => 'e.time_created DESC',

    Just try with appending either ASC or DESC to the `group_by` and `order_by ' options values to see how it changes the order.

  • Im trying to use this example I found :

    SELECT max(id) as id, 
       asker
    FROM questions 
    GROUP by asker 
    ORDER by id DESC
    

    Implemented in Elgg like the following:

    $content = elgg_list_entities(array(
        'type' => 'object',
        'subtype' => 'file',
        'selects' => array("max(e.time_created) AS createdtime"),
        'order_by' => 'createdtime ASC',
        'group_by' => 'e.owner_guid',
    ));

    But this doesn't seem to work still.  Any ideas?