How can I get all related entities of a node programmatically? I am writing some drush scripts and find myself needing to get related entities (nodes) of the current node. However I can only find info for constructing views that do this.

Can anyone tell me is there a programmatic way to pull all entities that relate to the current nid with a filter on the entity type?

Ultimately I need to get a list of node ids that relate to the current node id.

The relationship depend on the relation module.
Like this, you will get all the referenced items of the node being displayed.

  $current_path = explode('/', $_GET['q']);
  if ($current_path[0] == 'node' &amp;&amp; is_numeric($nid = $current_path[1])) {
    $field = 'my_field';
    $bundle = 'my_bundle';
    $query = db_query("SELECT {$field}_target_id FROM field_data_{$field} WHERE entity_id = :nid AND bundle = :bundle",
      array(
        ':nid' =&gt; $nid,
        ':bundle' =&gt; $bundle,
      )
    );
    foreach($query as $row) {
      $target_ids[] = $row-&gt;{$field . "_target_id"};
    }
    $nodes = node_load_multiple($target_ids);
  }


If you have more entity reference fields, you have to expand the query (with more joins) according to your fields.