As opposed to the other answer, if you need it programmatically :
a quite dynamic solution (a bit dirty too but I needed it quickly) so you do not need to hard code the name fo the referencing field and it is automatically handled with new referencing field you will add in the future :
in your custom module :
/**
* Implement hook_field_create_instance().
*/
function MY_CUSTOM_MODULE_field_create_instance() {
_MY_CUSTOM_MODULE_set_variable_node_back_references();
}
/**
* Implement hook_field_delete_field().
*/
function MY_CUSTOM_MODULE_field_delete_field() {
_MY_CUSTOM_MODULE_set_variable_node_back_references();
}
/**
* Set Variable node_back_references.
*/
function _MY_CUSTOM_MODULE_set_variable_node_back_references() {
$field_list = db_select('field_config', 'fc')
->fields('fc', array('field_name', 'data'))
->condition('fc.data', '%"foreign keys";a:1:{s:4:"node"%', 'like')
->condition('fc.deleted', 0);
$field_list->innerJoin('field_config_instance', 'fci', 'fci.field_name = fc.field_name');
$field_list->rightJoin('node_type', 'n', 'n.type = fci.bundle');
$fields = $field_list->execute()->fetchAll();
$fields_array = array();
foreach ($fields as $field) {
$unserialized = unserialize($field->data);
if (isset($unserialized['settings']['handler_settings']['target_bundles'])) {
foreach ($unserialized['settings']['handler_settings']['target_bundles'] as $bundle) {
$fields_array[$bundle][] = $field->field_name;
}
}
}
variable_set('node_back_references', $fields_array);
}
function _MY_CUSTOM_MODULE_get_referencing_nodes($node) {
$nids = array();
$fields = variable_get('node_back_references', array());
if (isset($fields[$node->type])) {
foreach ($fields[$node->type] as $field) {
$query = new \EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
$query->propertyCondition('status', 1);
$query->fieldCondition($field, 'target_id', $node->nid);
$result = $query->execute();
$nids = isset($result['node']) ? array_merge(array_keys($result['node']), $nids) : $nids;
}
$nodes = (!empty($nids)) ? node_load_multiple($nids) : array();
return $nodes;
}
return $nids;
}
where you need to get the parent nodes given the child node :
$nodes = _MY_CUSTOM_MODULE_get_referencing_nodes($node);