$metas = $this->get_metas_by_post_id($ids, $cf_names);
public function get_metas_by_post_id($post_id, $cf_names = null)
{
global $wpdb;
$sql = array();
$sql[] = "SELECT";
$fields = array();
$fields[] = "pm.post_id AS ID";
$fields[] = "pm.meta_key";
$fields[] = "pm.meta_value";
$fields[] = "(CASE WHEN p2.guid IS NULL THEN '' ELSE p2.guid END) AS guid";
$sql[] = implode(',', $fields);
$sql[] = "FROM $wpdb->postmeta AS pm";
$sql[] = "LEFT JOIN $wpdb->posts AS p ON p.ID = pm.post_id";
$sql[] = "LEFT JOIN $wpdb->posts AS p2 ON pm.meta_value = p2.ID AND p2.post_type = 'attachment'";
$sql[] = "WHERE pm.post_id " . $this->_get_sql_for_in($post_id, true);
if (!empty($cf_names))
$sql[] = "AND pm.meta_key " . $this->_get_sql_for_in($cf_names);
$sql[] = "ORDER BY pm.post_id ASC";
$result = $wpdb->get_results(implode(' ', $sql));
$this->unserialize_by_keys($result);
return $result;
}
public function unserialize_by_keys(&$post, $keys = null)
{
if (is_array($post)) {
foreach ($post AS $elm)
$this->unserialize_by_keys($elm, $keys);
return;
}
$target = !empty($keys) ? $keys : $post;
foreach ($target as $key => $value)
$post->$key = maybe_unserialize($post->$key);
}