How to programmatically add a filter that requires a relationship to a view I have a view filtering out a specific node by nid, and it was working until I added a relationship. I want to filter based on the nid of the target of the relationship, not the base table nid. Here is my code, pre-relationship:

$options = array(
  'operator' =&gt; '!=',
  'value' =&gt; array('value' =&gt; $nid),
);
$view-&gt;add_item ($display_name, 'filter', 'node', 'nid', $options);


I tried replacing nid with the view relationship base field alias, but that didn't seem to be recognized at all. How do I indicate to Views that this filter should work via the relationship?
I figured this out by a stroke of luck after posting this. Here is what I did:

// Get a list of all relationships on the view display
$relationships = $view-&gt;get_items('relationship', $display_name);
// In case there are no relationships, set this to null by default.
$last_relationship = null;
if (!empty($relationships)) {
  // @todo: is the last relationship always the one whose ID goes with the returned entities?
  $last_relationship = array_pop($relationships);
}
// Filter out the (un)desired item.
$options = array(
  'operator' =&gt; '!=',
  'value' =&gt; array('value' =&gt; $id),
);
if ($last_relationship) {
  $options['relationship'] = $last_relationship['id'];
}
$view-&gt;add_item($display_name, 'filter', $view-&gt;base_table, $view-&gt;base_field, $options);