4

I would like to get values of a field from all nodes of a content type. How can I do this? EntityFieldQuery seems to return only node ids, so will I need db_query? Sg similar to this In a custom module how do I get all the nodes of a certain type?

I guess I just need to lookup the table name of the field...

1 Answer 1

5

In your case, you can probably simply query the field data table, with a condition on the entity type and bundle.

In any case, something like:

$field_name = 'FIELD_NAME';
$result = db_select("field_data_{$field_name}", 'f')
  ->fields('f', array('entity_id', "{$field_name}_value"))
  ->condition('entity_type', 'node')
  ->condition('bundle', 'article')
  ->condition('deleted', 0)
  ->range(0,10)
  ->execute();

foreach ($result as $row) {
  // drupal_set_message($row->field_name_value); // example
  drupal_set_message('<pre>'.print_r($row,1).'</pre>');
}

As an aside you might want to join on the node table to only select published content, but that's the gist of it.

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.