acf_update_metadata_by_field()ACF 6.4

Updates metadata in the database.

Хуки из функции

Возвращает

Int|true|false. Meta ID if the key didn't exist, true on successful update, false on failure.

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

acf_update_metadata_by_field( $post_id, $field, $value, $hidden );
$post_id(int|строка)
The post id.
$field(массив)
The field array.
По умолчанию: array()
$value(разное)
The meta value.
По умолчанию: ''
$hidden(true|false)
True if we should update the reference key.
По умолчанию: false

Список изменений

С версии 6.4 Введена.

Код acf_update_metadata_by_field() ACF 6.4.2

function acf_update_metadata_by_field( $post_id = 0, $field = array(), $value = '', bool $hidden = false ) {
	if ( empty( $field['name'] ) ) {
		return null;
	}

	// Allow filter to short-circuit logic.
	$pre = apply_filters( 'acf/pre_update_metadata', null, $post_id, $field['name'], $value, $hidden );
	if ( $pre !== null ) {
		return $pre;
	}

	// Decode the $post_id for $type and $id.
	$decoded = acf_decode_post_id( $post_id );
	$id      = $decoded['id'];
	$type    = $decoded['type'];

	// Bail early if no $id (possible during new acf_form).
	if ( ! $id ) {
		return false;
	}

	$meta_instance = acf_get_meta_instance( $type );

	if ( ! $meta_instance ) {
		return false;
	}

	if ( $hidden ) {
		return $meta_instance->update_reference( $id, $field['name'], $field['key'] );
	}

	return $meta_instance->update_value( $id, $field, $value );
}