Luke Snowden
Luke Snowden

Reputation: 4196

Drupal 7 - entityFieldQuery Order

Morning, im trying to get a set of latest node types but cant seem to figure out how to order them by date. Heres my function so far:

function latest_nodes($type, $limit = 15, $offset = 0) {
    $query = new EntityFieldQuery();
    $tmp = $query->entityCondition('entity_type', 'node');
    if( is_string( $type ) )
        $tmp->entityCondition('bundle', $type);
    elseif( is_array( $type ) )
        $tmp->entityCondition('bundle', $type, 'IN');
    $tmp->range($offset, $limit);
    $results = $tmp->execute();
    return node_load_multiple(array_keys($results['node']));
}

any help would be muchly appreciated!

Upvotes: 16

Views: 22165

Answers (3)

elaz
elaz

Reputation: 121

to retrieve the latest post you can use

$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
                  ->entityCondition('bundle', 'your_content_type')
                  ->propertyCondition('status', 1)
                  ->propertyOrderBy('created', 'DESC');

Upvotes: 0

mikeytown2
mikeytown2

Reputation: 1764

You might be looking for propertyOrderBy

$query = new EntityFieldQuery();
$query->propertyOrderBy('changed', 'DESC');

Upvotes: 23

Clive
Clive

Reputation: 36955

You're looking for the fieldOrderBy() member function, e.g.

$query = new EntityFieldQuery();
$query->fieldOrderBy('field_name_of_field', 'value', 'DESC');

Upvotes: 25

Related Questions