How should I get the value contained in a particular field of a Drupal 7 custom node?

11,022

Not sure why EntityFieldQuery is being discussed, but ok. :) You're actually going to want to use the field_get_items() function.

if ($nodes = node_load_multiple(array(), array('type' => 'custom', 'title' => $title)) {
  $node = reset($nodes);
  if ($items = field_get_items('node', $node, 'url')) {
    $url = $items[0]['value'];
    // Do whatever
  }
}
Share:
11,022
Matt V.
Author by

Matt V.

Matt Vance is a Web developer, technology consultant, and freelance writer living in Austin, Texas. He has written for Macworld magazine, Lifehacker.com, and DZone.

Updated on June 04, 2022

Comments

  • Matt V.
    Matt V. almost 2 years

    What is the "proper" way to get the value stored in a particular field within a custom Drupal node? I've created a custom module, with a custom node, with a custom URL field. The following works:

    $result = db_query("SELECT nid FROM {node} WHERE title = :title AND type = :type", array(
      ':title' => $title,
      ':type' => 'custom',
    ))->fetchField();
    $node = node_load($result);
    $url = $node->url['und']['0']['value'];
    

    ...but is there a better way, maybe using the new Field API functions?