I have the following function that simply grabs nodes:

function getWords() {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'node', '=')
            ->propertyCondition('status', 1, '=')
            ->propertyCondition('type', 'wall_of_words', '=');
    $result = $query->execute();
    return node_load_multiple(array_keys($result['node']));


Right now it's working but it's returning everything, is there a way I can return the contents of the body or title field only?

  • 2
    Currently, that code would return the fully loaded node objects, if you want just certain fields, you'd probably need to iterate over them and build a new array of just the relevant data, e.g title and body. Given you already have the full node, perhaps better to just return them and filter the field data from the calling function instead. Feb 12, 2013 at 23:07
  • I am not sure, but I think that doing ->entityCondition('bundle', 'wall_of_words') is preferred over ->propertyCondition('type', 'wall_of_words', '=') for limiting to a particular content type.
    – mpdonadio
    Feb 13, 2013 at 14:49

2 Answers 2


You can leverage field_attach_load() for this:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node', '=')
  ->propertyCondition('status', 1, '=')
  ->propertyCondition('type', 'wall_of_words', '=');
$result = $query->execute();

$nodes = $result['node'];

// Get all fields attached to a given node type
$fields = field_info_instances('node', 'wall_of_words');

// Get id of body field
$field_id = $fields['body']['field_id'];

// Attach a field of selected id only to get value for it
field_attach_load('node', $nodes, FIELD_LOAD_CURRENT, array('field_id' => $field_id));

Now you have $nodes array of "half-loaded" nodes, where you can get your field data from like it were a usual node, just do something like this:

foreach ($nodes AS $nid => $node) {
  print $node->body['und']['value'];

Note. I used 'und' for language here. You may want to query for node language field here to get a dynamic language data if needed.

So this should work.

UPD. One note to this. field_attach_load() doesn't invoke hook_node_load(), so if some data is being manipulated via hook_node_load() you won't get its representation via described method. This is the only drawback of this method. But in most cases it is safe to use. Just check if it works in your circumstances.


If I were only interested in one or two fields, I would use db_select(), as in the following code.

$result = db_select('node', 'n')
  ->fields('n', array('title', 'body'))
  ->condition('type', 'wall_of_words' ,'=')
  ->condition('status', 1 ,'=')

However, I tend to design with future modifications in mind. I think that using an EFQ to do the query and then using entity_metadata_wrapper() to extract the fields makes the code more readable and changeable in the future. Unless this causes large performance impacts, this is a big benefit to me.

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.