/** * Ajax callback for assign posts auto complete */ public static function ajaxAssignPostsData($types) { $results = array(); if (strlen($_GET['term']) > 0) { // Go directly to the database $sql = ' SELECT ID, post_title FROM {sql:postTable} WHERE (ID = {postId} OR post_title LIKE {postTitle}) AND post_type IN({sql:postTypes}) '; global $wpdb; $posts = $wpdb->get_results(String::prepareSql($sql, array('postTable' => $wpdb->posts, 'postId' => intval($_GET['term']), 'postTitle' => '%' . $_GET['term'] . '%', 'postTypes' => '"' . implode('","', $types) . '"'))); foreach ($posts as $post) { $results[] = array('id' => $post->ID, 'value' => $post->post_title, 'label' => $post->post_title); } } header('Content-Type: application/json'); echo json_encode($results); exit; }