I see Documentation of entity_load but I didn't find anything about ordering of results, How can I order the results entity_load by delta?

as example I fetch all product of one node by

$node = node_load(313);
$field = field_view_field('node', $node, 'field_product_select');
$pids = array();
foreach ($field['#items'] as $key => $value) {
  $pids[] = $value['product_id'];
$products = entity_load('commerce_product', $pids, array(), TRUE);

$return = array();
foreach ((array) $products as $key => $product) {
  $return[$key]['product_id'] = $key;
  $return[$key]['product_type'] = $product->type;
  $return[$key]['price'] = $product->commerce_price['und'][0]['amount'];
  $images = field_view_field('commerce_product', $product, 'field_product_images');
  foreach ((array) $images['#items'] as $image) {
    $return[$key]['img'][] = $image['uri'];

I want fetch products order by 'delta'(or other field ),(results are order by entity_id by default) , how can I achieve it?

2 Answers 2


Most entity controller implementations of ::load() will return entities in the same order as the array of IDs passed in to be loaded. This is the case with DrupalDefaultEntityController and EntityAPIController, which are the two most common controllers.

In this case you could structure your initial query to get a list of IDs to be loaded with EntityFieldQuery and then use the results of that to pass to entity_load.

Looking at your original example, you just need to load a list of products based on the product field on a node. Here's a quick example of what it would take to handle that (since you don't need EntityFieldQuery):

$node = node_load($nid); // If you don't already have access to the node
$wrapper = entity_metadata_wrapper('node', $node);

$product_ids = $wrapper->field_product_select->raw();
$products = entity_load('commerce_product', $product_ids);

This works because the field data is always queried with order by delta when using Drupal core's field SQL storage: https://api.drupal.org/api/drupal/modules%21field%21modules%21field_sql_storage%21field_sql_storage.module/function/field_sql_storage_field_storage_load/7

  • @mikev P, tnx for answer, I complete it with implementation of my solution
    – Yuseferi
    Apr 17, 2015 at 6:14

You can load each product separately

foreach ($field['#items'] as $key => $value) {
  $products[] = entity_load('commerce_product', array($value['product_id']), array(), TRUE);
  • Depending on the number of fields on the product and the number of products to load, this could cause a larger than necessary number of queries to be run. See What is the n + 1 select issue for more information.
    – Mikey P
    Apr 27, 2015 at 23:45
  • Yes, I understand that it's not the best solution. Thank you.
    – Eugene
    Apr 28, 2015 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.