Changing groups sort order from "alphabetical" to "most recent"

I'm using a custom plugin to refine (and constrain) the information that users see on their landing page when they do a login. At the moment, the main area consists simply of a list of groups to which they belong.

The code that produces this is:

// groups

if (elgg_is_active_plugin('groups')) {

echo elgg_view_module('featured',  elgg_echo("groups:yours"), $vars['groups'], $mod_params);

}
And (as mentioned) that gives me an alphabetical list of groups, but "most recent" is the desired sort order.
 
We use the "group tools" plugin, too, and in its "Group listing settings" I have the "Newest" radio button selected for "Sort" for each option, so clearly that's not having any effect on this landing page listing. (It does, however, work for clicking on tabs, etc., on the homepage.)
 

Hopefully there's a simple tweak to the code provided above, but I can't work out what that would be. Thanks for any help with this!

  • $mod_params is defined in the top of the PHP file this way:

    $mod_params = array('class' => 'elgg-module-highlight');
    

    This variable also appears in some other lines that I don't make use of, e.g.:

    echo elgg_view_module('featured',  '', $top_box, $mod_params);

    (I have adapted this code from the standard front page ... I think! :( I set it up a long time ago.)

  • I've missed $vars['groups'] ...

    Shortly, you should use this code:

    $options = [
        'type' => 'group',
        'full_view' => false,
        'no_results' => elgg_echo('groups:none'),
        'relationship' => 'member',
        'relationship_guid' => elgg_get_logged_in_user_guid(),
        'inverse_relationship' => false,
    ];
    
    $groups = elgg_list_entities_from_relationship($options);

    Elgg default sort the groups by time created.

    I don't know where and how you use this module but you can try:

    echo elgg_view_module('featured',  elgg_echo("groups:yours"), $groups, $mod_params);
  • I've missed $vars['groups'] ...

    Shortly, you should use this code:...

    Ah! Of course -- that was the pointer I needed. I found the $vars['groups'] values, but still don't know the "order-by" value I should set. Currently I've got:

      'order_by' => 'ge.name ASC'

    What should it be for my desired sort order? (And thank you for your quick replies - much appreciated!)

  • Or remove this parameter, or use this:

    'order_by' => 'ge.time_created desc',
  • Brilliant! Exactly what I needed - many thanks!