diff --git a/include/ajax.tickets.php b/include/ajax.tickets.php
index 45eb78ecee1e21d4c5a88d55c7641727185a59dd..4cd6829967f982ee538d613f1beac53225d7c979 100644
--- a/include/ajax.tickets.php
+++ b/include/ajax.tickets.php
@@ -202,14 +202,6 @@ class TicketsAjaxAPI extends AjaxController {
             $criteria['created__lte'] = $startTime;
         }
 
-        //Query
-        $joins = array();
-        if($req['query']) {
-            // Setup sets of joins and queries
-            if ($s = $ost->searcher)
-               return $s->find($req['query'], $criteria, 'Ticket');
-        }
-
         // Dynamic fields
         $cdata_search = false;
         foreach (TicketForm::getInstance()->getFields() as $f) {
@@ -218,10 +210,14 @@ class TicketsAjaxAPI extends AjaxController {
                     && strlen(trim($val))) {
                 $name = $f->get('name') ? $f->get('name')
                     : 'field_'.$f->get('id');
-                if ($f->getImpl()->hasIdValue() && is_numeric($val))
+                if ($f->getImpl()->hasIdValue() && is_numeric($val)) {
                     $cwhere = "cdata.`{$name}_id` = ".db_input($val);
-                else
+                    $criteria["cdata.{$name}_id"] = $val;
+                }
+                else {
                     $cwhere = "cdata.`$name` LIKE '%".db_real_escape($val)."%'";
+                    $criteria["cdata.{$name}"] = $val;
+                }
                 $where .= ' AND ('.$cwhere.')';
                 $cdata_search = true;
             }
@@ -230,6 +226,14 @@ class TicketsAjaxAPI extends AjaxController {
             $from .= 'LEFT JOIN '.TABLE_PREFIX.'ticket__cdata '
                     ." cdata ON (cdata.ticket_id = ticket.ticket_id)";
 
+        //Query
+        $joins = array();
+        if($req['query']) {
+            // Setup sets of joins and queries
+            if ($s = $ost->searcher)
+               return $s->find($req['query'], $criteria, 'Ticket');
+        }
+
         $sections = array();
         foreach ($joins as $j) {
             $sections[] = "$select $from {$j['from']} $where AND ({$j['where']})";
diff --git a/include/class.search.php b/include/class.search.php
index 914bb2c1342af38299f653e56270350baecf80ca..55fc7cf89a580fd15b2876c29f347ac0ddf58b46 100644
--- a/include/class.search.php
+++ b/include/class.search.php
@@ -309,9 +309,19 @@ class MysqlSearchBackend extends SearchBackend {
                     case 'email':
                     case 'org_id':
                     case 'form_id':
+                    default:
+                        if (strpos($name, 'cdata.') === 0) {
+                            // Search ticket CDATA table
+                            $cdata_search = true;
+                            $name = substr($name, 6);
+                            $where[] = sprintf("cdata.%s = %s", $name, db_input($value));
+                        }
                     }
                 }
             }
+            if ($cdata_search)
+                $tables[] = TABLE_PREFIX.'ticket__cdata cdata'
+                    .' ON (cdata.ticket_id = A1.ticket_id)';
 
             // Always consider the current staff's access
             $thisstaff->getDepts();