diff --git a/realname.module b/realname.module
index 6384e7d..b02b2fd 100644
--- a/realname.module
+++ b/realname.module
@@ -507,3 +507,35 @@ function realname_username_raw_token_validate(&$element, &$form_state) {
 
   return $element;
 }
+
+/**
+ * Implements hook_query_TAG_alter().
+ */
+function realname_query_era_query_alter(QueryAlterableInterface $query) {
+  // Even though this tag should't be called elsewhere, just double check
+  if (module_exists('entityreference_autocomplete')) {
+    $efq = $query->getMetaData('entity_field_query');
+
+    // Add compatability for realname
+    $entity_type = $efq->entityConditions['entity_type']['value'];
+
+    if ($entity_type == 'user') {
+      // Remove the current condition to the label
+      $label_column = entityreference_autocomplete_resolve_entity_label_column($entity_type);
+      $where =& $query->conditions();
+      foreach (element_children($where) as $key) {
+        if (is_string($where[$key]['field']) && $where[$key]['field'] == 'users.' . $label_column) {
+          unset($where[$key]);
+        }
+      }
+
+      // Add the conditon for realname
+      $string = $query->getMetaData('era_search_string');
+      $query->leftJoin('realname', 'rn', 'users.uid = rn.uid');
+      $query->condition(db_or()
+        ->condition('rn.realname', db_like($string) . '%', 'LIKE')
+        ->condition('users.name', db_like($string) . '%', 'LIKE')
+      );
+    }
+  }
+}
