diff --git a/mappers/taxonomy.inc b/mappers/taxonomy.inc
index 088a55897740858860222fc11eb21340c4b8a8af..1a83e0b3264f37be6a22972a6513307ed8e5484b 100644
--- a/mappers/taxonomy.inc
+++ b/mappers/taxonomy.inc
@@ -136,7 +136,14 @@ function taxonomy_feeds_set_target($source, $entity, $target, array $terms, $map
           $term = trim($term);
           $name_query = clone $query;
           if (strlen($term) && $tids = $name_query->propertyCondition('name', $term)->execute()) {
-            $tid = key($tids['taxonomy_term']);
+
+            // Find the first allowed term.
+            foreach ($tids['taxonomy_term'] as $possible_term) {
+              if (isset($cache['allowed_values'][$target][$possible_term->tid])) {
+                $tid = $possible_term->tid;
+                break;
+              }
+            }
           }
           elseif ($mapping['autocreate'] && strlen($term)) {
             $term = (object) array(
@@ -154,7 +161,7 @@ function taxonomy_feeds_set_target($source, $entity, $target, array $terms, $map
         // Lookup by tid.
         case FEEDS_TAXONOMY_SEARCH_TERM_ID:
           if (is_numeric($term)) {
-            $tid = $term;
+            $tid = (int) $term;
           }
           break;