diff --git a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
index 2161338..02fccea 100644
--- a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
+++ b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
@@ -263,9 +263,8 @@ class EntityReference_SelectionHandler_Generic_node extends EntityReference_Sele
     // 'unpublished'. We need to do that as long as there are no access control
     // modules in use on the site. As long as one access control module is there,
     // it is supposed to handle this check.
-    if (!user_access('bypass node access') && !count(module_implements('node_grants'))) {
-      $tables = $query->getTables();
-      $query->condition(key($tables) . '.status', NODE_PUBLISHED);
+    if (!user_access('bypass node access') && !count(module_implements('node_grants')) && $base_table = _entityreference_selection_handler_get_base_table($query, 'node')) {
+      $query->condition($base_table . '.status', NODE_PUBLISHED);
     }
   }
 }
@@ -338,16 +337,16 @@ class EntityReference_SelectionHandler_Generic_comment extends EntityReference_S
     // Adding the 'comment_access' tag is sadly insufficient for comments: core
     // requires us to also know about the concept of 'published' and
     // 'unpublished'.
+    if (!$base_table = _entityreference_selection_handler_get_base_table($query, 'comment')) {
+      return;
+    }
     if (!user_access('administer comments')) {
-      $tables = $query->getTables();
-      $query->condition(key($tables) . '.status', COMMENT_PUBLISHED);
+      $query->condition($base_table . '.status', COMMENT_PUBLISHED);
     }
 
     // The Comment module doesn't implement any proper comment access,
     // and as a consequence doesn't make sure that comments cannot be viewed
     // when the user doesn't have access to the node.
-    $tables = $query->getTables();
-    $base_table = key($tables);
     $node_alias = $query->innerJoin('node', 'n', '%alias.nid = ' . $base_table . '.nid');
     // Pass the query to the node access control.
     $this->reAlterQuery($query, 'node_access', $node_alias);
@@ -384,10 +383,11 @@ class EntityReference_SelectionHandler_Generic_comment extends EntityReference_S
  */
 class EntityReference_SelectionHandler_Generic_file extends EntityReference_SelectionHandler_Generic {
   public function entityFieldQueryAlter(SelectQueryInterface $query) {
+    if (!$base_table = _entityreference_selection_handler_get_base_table($query, 'file_managed')) {
+      return;
+    }
     // Core forces us to know about 'permanent' vs. 'temporary' files.
-    $tables = $query->getTables();
-    $base_table = key($tables);
-    $query->condition('status', FILE_STATUS_PERMANENT);
+    $query->condition($base_table . 'status', FILE_STATUS_PERMANENT);
 
     // Access control to files is a very difficult business. For now, we are not
     // going to give it a shot.
@@ -409,11 +409,12 @@ class EntityReference_SelectionHandler_Generic_file extends EntityReference_Sele
  */
 class EntityReference_SelectionHandler_Generic_taxonomy_term extends EntityReference_SelectionHandler_Generic {
   public function entityFieldQueryAlter(SelectQueryInterface $query) {
+    if (!$base_table = _entityreference_selection_handler_get_base_table($query, 'taxonomy_vocabulary')) {
+      return;
+    }
     // The Taxonomy module doesn't implement any proper taxonomy term access,
     // and as a consequence doesn't make sure that taxonomy terms cannot be viewed
     // when the user doesn't have access to the vocabulary.
-    $tables = $query->getTables();
-    $base_table = key($tables);
     $vocabulary_alias = $query->innerJoin('taxonomy_vocabulary', 'n', '%alias.vid = ' . $base_table . '.vid');
     $query->addMetadata('base_table', $vocabulary_alias);
     // Pass the query to the taxonomy access control.
@@ -431,3 +432,24 @@ class EntityReference_SelectionHandler_Generic_taxonomy_term extends EntityRefer
     }
   }
 }
+
+
+/**
+ * Helper function; Return the base table alias from a query.
+ *
+ * @param SelectQueryInterface $query
+ *   The query object.
+ * @param $table_name
+ *   The non-aliased name of the base table.
+ *
+ * @return
+ *   The alias of the table, or FALSE is not found.
+ */
+function _entityreference_selection_handler_get_base_table(SelectQueryInterface $query, $table_name) {
+  foreach ($query->getTables() as $key => $table) {
+    if ($table['table'] == $table_name) {
+      return $key;
+    }
+  }
+  return FALSE;
+}
