Automattic\WooCommerce\Internal\DataStores
CustomMetaDataStore::get_meta_data_for_object_ids
Return order meta data for multiple IDs.
Method of the class: CustomMetaDataStore{}
No Hooks.
Returns
\stdClass[][]. An array, keyed by object_ids, containing array of raw meta data records for each object. Objects with no meta data will have an empty array.
Usage
$CustomMetaDataStore = new CustomMetaDataStore(); $CustomMetaDataStore->get_meta_data_for_object_ids( $object_ids ): array;
- $object_ids(array) (required)
- List of object IDs.
CustomMetaDataStore::get_meta_data_for_object_ids() CustomMetaDataStore::get meta data for object ids code WC 10.3.6
public function get_meta_data_for_object_ids( array $object_ids ): array {
global $wpdb;
if ( empty( $object_ids ) ) {
return array();
}
$id_placeholder = implode( ', ', array_fill( 0, count( $object_ids ), '%d' ) );
$meta_table = $this->get_table_name();
$object_id_column = $this->get_object_id_field();
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $object_id_column and $meta_table is hardcoded. IDs are prepared above.
$meta_rows = $wpdb->get_results(
$wpdb->prepare(
"SELECT id, $object_id_column as object_id, meta_key, meta_value FROM $meta_table WHERE $object_id_column in ( $id_placeholder )",
$object_ids
)
);
// phpcs:enable
$meta_data = array_fill_keys( $object_ids, array() );
foreach ( $meta_rows as $meta_row ) {
if ( ! isset( $meta_data[ $meta_row->object_id ] ) ) {
$meta_data[ $meta_row->object_id ] = array();
}
$meta_data[ $meta_row->object_id ][] = (object) array(
'meta_id' => $meta_row->id,
'meta_key' => $meta_row->meta_key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $meta_row->meta_value, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
);
}
return $meta_data;
}