Hi,

I am using:

node_load_multiple(array(), array('type' => 'products'));

which returns an array of products and works ok. Is there any way i can sort it by added date?

Thanks,
Pav

Comments

tecjam’s picture

As the api for node_load_multiple() explains, the conditions parameter is depreciated. So you should first use EntityFieldQuery to retrieve a list of entity IDs loadable by this function. Use EntityFieldQuery to build your query and order the results.

eg:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->propertyCondition('status', 1)
  ->propertyCondition('type', array('article', 'page', 'blog'))
  ->propertyOrderBy('created', 'DESC')
  ->range(0, 5);
$result = $query->execute();

This returns the last 5 published nodes of types article, page and blog. Then you can use these returned nid's to load them each using node_load_multiple().

See here for another example: http://drupal.org/node/916776#comment-4191166

Lams’s picture

The final line of code for this is:

$myPosts = node_load_multiple(array_keys($result['node'])); 
arctirux’s picture

This should do the trick

<?php
function YOUR_MODULE_node_load_multiple($x,$d){
 $query = new EntityFieldQuery(); $d=(object)$d;
 $query->entityCondition('entity_type','node')
 ->propertyCondition('type',$d->type)
 ->propertyOrderBy($d->sortbyfield,'ASC');
  $result=$query->execute();
  return $result['node'];	 
 }
?>
vinoth.babu’s picture

Please try this also will help


$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', '<CONTENT TYPE>') // ex. article
  ->propertyCondition('uid', $uid) // node written by a specific user
  ->propertyCondition('status', 1) // published nodes
  ->propertyOrderBy('created', 'DESC');
$result = $query->execute();
 
if (isset($result['node'])) {
  $nids = array_keys($result['node']);
  return node_load_multiple($nids);
} else {
  return array();
}