WC_Post_Data::update_post_meta_batch │ private static │ WC 1.0

Update post metadata in one query, preserving concurrent edits.

Each CASE branch assigns a replacement only when the row still holds its original value, compared under the column's collation; ELSE keeps newer values. Writes bypass metadata hooks. The caller must invalidate the affected post meta caches.

Метод класса: WC_Post_Data{}

Хуков нет.

Возвращает

bool. Whether the query succeeded.

Использование

$result = WC_Post_Data::update_post_meta_batch( $updates ): bool;
$updates(array<int, array{old_value: string, new_value: string}>) (обязательный)
Original and replacement stored values, keyed by metadata ID.

Код WC_Post_Data::update_post_meta_batch() WC 11.2.2

private static function update_post_meta_batch( array $updates ): bool {
	global $wpdb;

	if ( ! $updates ) {
		return true;
	}

	$cases = array();
	foreach ( $updates as $meta_id => $values ) {
		$cases[ (int) $meta_id ] = $wpdb->prepare(
			'WHEN meta_id = %d AND meta_value = %s THEN %s',
			$meta_id,
			$values['old_value'],
			$values['new_value']
		);
	}

	return false !== $wpdb->query( "UPDATE {$wpdb->postmeta} SET meta_value = CASE " . implode( ' ', $cases ) . ' ELSE meta_value END WHERE meta_id IN (' . implode( ',', array_keys( $cases ) ) . ')' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Built from prepared fragments and integer IDs.
}