As always, use the source. It is well commented.
In particular, you will want to use the order_by option (defined in elgg_get_entities) or the order_by_metadata option. See the excerpt from engine/lib/metadata.php for that.
/**
* Returns entities based upon metadata. Also accepts all
* options available to elgg_get_entities(). Supports
* the singular option shortcut.
*
* NB: Using metadata_names and metadata_values results in a
* "names IN (...) AND values IN (...)" clause. This is subtly
* differently than default multiple metadata_name_value_pairs, which use
* "(name = value) AND (name = value)" clauses.
*
* When in doubt, use name_value_pairs.
*
* @see elgg_get_entities
* @see elgg_get_entities_from_annotations
* @param array $options Array in format:
*
* metadata_names => NULL|ARR metadata names
*
* metadata_values => NULL|ARR metadata values
*
* metadata_name_value_pairs => NULL|ARR (name => 'name', value => 'value', 'operand' => '=', 'case_sensitive' => TRUE) entries.
* Currently if multiple values are sent via an array (value => array('value1', 'value2') the pair's operand will be forced to "IN".
*
* metadata_name_value_pairs_operator => NULL|STR The operator to use for combining (name = value) OPERATOR (name = value); default AND
*
* metadata_case_sensitive => BOOL Overall Case sensitive
*
* order_by_metadata => NULL|ARR (array('name' => 'metadata_text1', 'direction' => ASC|DESC, 'as' => text|integer),
* Also supports array('name' => 'metadata_text1')
*
* metadata_owner_guids => NULL|ARR guids for metadata owners
*
* @return array
* @since 1.7.0
*/
This is along the same question. Could I use metadata fields instead of register/extend for registration? That'd be easier!
I have a problem with this. I am trying to sort users by their last name which is a metadata.
$select_arr['order_by_metadata'] = array( 'name' => "lastname", 'direction' => "asc" );
But this would show only users who have entered their last name. The other users are not shown. Is there any option to show them too?
Title is not stored as metadata, it is part of the core objects_entity table. You'll have to use a custom join and order_by clause. Something like this (untested):
$calendars = elgg_get_entities_from_metadata(array(
'types' => 'object',
'subtypes' => '3key_calendar',
'owner_guid' => $owner->guid,
'container_guids' => $owner->guid,
'joins' => array("INNER JOIN {$CONFIG->dbprefix}objects_entity o ON (e.guid = o.guid)"),
'order_by' => 'o.title'
));
Thank Andras... this code works for me now... thanks a lot
info@elgg.org
Security issues should be reported to security@elgg.org!
©2014 the Elgg Foundation
Elgg is a registered trademark of Thematic Networks.
Cover image by Raül Utrera is used under Creative Commons license.
Icons by Flaticon and FontAwesome.