How to programmatically add a filter that requires a relationship to a view
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);