Sort subtype entities

Hello -

Can anyone please assist me in explaining how to sort a list of subtype entities by the value of one of the subtypes' properties? I posted this question on Friday but didn't get a response yet.

I'm pretty new to Elgg and I'm not sure how to access the value of the property -- because I'm not exactly sure what type of object the value is.

The value is not metadata associated with the subtype, because I've tried using elgg_get_entities_from_metadata and order_by_metadata but those return no results.

I'm using elgg_list_entities and order_by to sort it like a multi-dimensional array, and this does seem to be doing some sorting … but it isn't allowing for the results to be sorted as intergers by asc and desc.


$content = elgg_list_entities(array(
    'type' => 'object',
    'subtype' => 'electrical',
    'full_view' => false,
    'view_toggle_type' => false,
    'limit' => 10,
    'pagination' => true,
     'order_by' => array(
        'name' => 'quantity',
        'direction' => 'DESC',
        'as' => 'integer'),
));

I've also tried sorting it like an entity (ie,  'order_by' => 'e.guid DESC'), but I'm not sure how to get at the value of it as related to a core entity with something like 'order_by' => 'e.subtype.value DESC'

The variable for the property that I'm trying to get at ('quantity') is entered by users into a form. In the plugin's lib, the variables for the value of form inputs are initially created as part of an array like this:

$sku = array(
      'title' => '',
      'code' => '',
      'description' => '',
      'quantity' => '',
  );


It's accessed elsewhere, for different views (ie, widgets and rss feeds), like this:
   $quantity = elgg_extract('quantity', $vars, '');
   $post->quantity


But I'm not sure how to access it within the elgg_list_entities function to allow for the correct sorting of the content. I have also tried order_by => $quantity, but that throws a fatal exception because the value of $quantity is not known.

Thanks for any help.

  • Assuming $post is an ElggObject of subtype 'electrical' and your code uses $post->quantity = $quantity; to assign the value, it is stored as metadata.

    You can use:


    $content = elgg_list_entities_from_metadata(array(
    'type' => 'object',
    'subtype' => 'electrical',
    'full_view' => false,
    'view_toggle_type' => false,
    'limit' => 10,
    'pagination' => true,
    'order_by_metadata' => array(
    'name' => 'quantity',
    'direction' => 'DESC',
    'as' => 'integer'),
    ));

  • Great - thanks so much. I had tried elgg_get_entities_from_metadata and didnt get any results but your code with elgg_list_entities_from_metadata works perfectly. Thanks!