2

I have added related nodes using Node Reference module provided by the references module.
Now I want to retrieve all the related nodes. So I am looking for a function which takes a node id as input and returns all the related nodes.

Is there any function provided by Drupal API or reference module which helps me getting nodes related to a node?

I tried searching in the module functions but I found nothing.

2

I don't know of an API function specifically for that but it's fairly simple to implement one using existing API methods:

function get_related_nodes($master_nid, $field_name) {
  // Load the 'master' node
  $master_node = node_load($master_nid);

  // Get the node reference items attached to this node
  $items = field_get_items('node', $node, $field_name);

  // Loop through and grab the nids
  $nids = array();
  foreach ($items as $item) {
    $nids[] = $item['nid'];
  }

  // Return the fully loaded reference nodes
  return node_load_multiple($nids);
}
  • Fist Many Thanks for the answer Clive..However if there were an inbuilt function it could have been cooler..This looks to be cool way..will implement it...Many Thanks....Smith – Smith Jun 21 '12 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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