diff --git a/realname.module b/realname.module
index a16be15..a56d437 100644
--- a/realname.module
+++ b/realname.module
@@ -433,3 +433,32 @@ function realname_username_raw_token_validate(&$element, &$form_state) {
 
   return $element;
 }
+
+/**
+ * Implements hook_query_TAG_alter().
+ */
+function realname_query_era_query_alter(QueryAlterableInterface $query) {
+  $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 ($where[$key]['field'] == 'users.' . $label_column) {
+        unset($where[$key]);
+      }
+    }
+
+    // Add the conditon for realname
+    $string = $query->getMetaData('era_search');
+    $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')
+    );
+  }
+}
