How can i order by metadata field?

i have been some trouble when i tried to get data with elgg_get_entities_from_metada, this doesn't have any order, including when i set order by parameter. How can i order a metadata field? i am trying with date field.



  • The default sequence is by date descending.
    There has been a lot of discussions before on this subject.
    Please just search around and you will find all the answers that you need ;-)

  • 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?