Autoprediction / Autocomplete Search with Meta Keys

If you want to show custom field values, you can’t use WP_Query that select posts.
It seems to me that the only available way is to write a sql query and execute it in $wpdb.

global $wpdb;

if ( isset($_POST['search']) == false || empty($_POST['search']) ) 
   wp_send_json_success( $array() ); 
$s = $_POST['search'];

$meta_keys = ['PLZ', 'ort', 'regionaler_zusatz'];
$limit = 100;
$sql = "SELECT meta_value FROM $wpdb->postmeta pm "
      . " WHERE pm.meta_key IN ('PLZ', 'ort', 'regionaler_zusatz') "
      . "    AND pm.meta_value LIKE %s LIMIT 0, %d ";
$args = ['%'.$s.'%', $limit];
$sql = $wpdb->prepare($sql, $args);
$res = $wpdb->get_results($sql, ARRAY_A);

$data = [];
if ( is_array($res) && !empty($res) ) {
   foreach( $res as $row )
       $data[] = $row['meta_value'];
   // -- or --
   //$data = array_reduce($res, function($carry, $item) {
   //    $carry[] = $item['meta_value'];
   //    return $carry;
   //}, []);
}
wp_send_json_success( $data );

If you don’t store in custom fields (PLZ, ort, regionaler_zusatz) single value, you may need to select matching item from the array.

tech