diff --git a/acf-autocomplete.php b/acf-autocomplete.php index 39c0597..5611622 100755 --- a/acf-autocomplete.php +++ b/acf-autocomplete.php @@ -32,15 +32,14 @@ public function autocomplete_ajax_callback() { //$results = array( 'post' => $_POST, 'get' => $_GET ); - $results = $wpdb->get_col( $wpdb->prepare( " - SELECT DISTINCT postmeta2.meta_value FROM $wpdb->postmeta as postmeta1, $wpdb->postmeta as postmeta2 WHERE postmeta1.meta_value = '%s' AND postmeta1.meta_key = CONCAT( '_', postmeta2.meta_key ) AND postmeta2.meta_value LIKE %s", - $_REQUEST['field_key'], - '%' . $_REQUEST['request'] . '%' - ) ); + $results = $wpdb->get_col( $wpdb->prepare( " + SELECT DISTINCT meta_value FROM {$wpdb->postmeta} + WHERE meta_key = '%s' + AND meta_value LIKE %s + ", $_REQUEST['field_key'], '%'.$_REQUEST['request'].'%' ) ); echo json_encode($results); - wp_die(); } diff --git a/js/input.js b/js/input.js index 29b4d0f..a4810ea 100755 --- a/js/input.js +++ b/js/input.js @@ -1,7 +1,7 @@ (function($){ - function initialize_field( $el ) { + function initialize_field( $el, field_name ) { $(':text:visible',$el).autocomplete({ source: function( request, response ){ @@ -11,7 +11,7 @@ $.getJSON( ajaxurl, { 'action' : 'autocomplete_ajax', - 'field_key' : $el.data('field_name'), + 'field_key' : field_name, 'request' : request.term.trim() }, function( data ){ @@ -33,7 +33,7 @@ // search $el for fields of type 'autocomplete' acf.get_fields({ type : 'autocomplete'}, $el).each(function(){ - initialize_field( $(this) ); + initialize_field( $(this), $(this).data('name') ); }); @@ -51,7 +51,7 @@ $(postbox).find('.field[data-field_type="autocomplete"]').each(function(){ - initialize_field( $(this) ); + initialize_field( $(this), $(this).data('field_name') ); });