_acf_get_stored_post_ids_snapshot() │ ACF 6.8.11

The internal ACF function to return the post IDs stored in an object's relational fields before the current user-submitted save changed them.

Repeater and Flexible Content rows are saved one at a time, and can be moved, added or removed, so a selection may be saved to a different row than it was stored in, after that row was already updated. The IDs are recorded, by field key, before the first relational field is updated, and kept until the save finishes. A sub field has the same key in every row, so this covers moved rows at any depth.

Внутренняя функция — эта функция рассчитана на использование самим ядром. Не рекомендуется использовать эту функцию в своем коде.

Хуков нет.

Возвращает

array. An array with ids, the sets of post IDs stored in each relational field, keyed by field key, and names, the meta names that have a field key reference.

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

_acf_get_stored_post_ids_snapshot( $post_id );
$post_id(int|строка) (обязательный)
The ACF post ID being saved.

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

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

Код _acf_get_stored_post_ids_snapshot() ACF 6.8.11

function _acf_get_stored_post_ids_snapshot( $post_id ) {
	$post_id   = _acf_get_stored_values_post_id( $post_id );
	$snapshots = (array) acf_get_data( 'acf_stored_post_ids_snapshots' );

	if ( ! isset( $snapshots[ $post_id ] ) ) {
		$meta       = (array) acf_get_meta( $post_id );
		$relational = array();
		$snapshot   = array(
			'ids'   => array(),
			'names' => array(),
		);

		// Each stored value has a reference to the key of the field it belongs to.
		foreach ( $meta as $name => $value ) {
			if ( ! isset( $meta[ '_' . $name ] ) ) {
				continue;
			}

			$field_key                  = $meta[ '_' . $name ];
			$snapshot['names'][ $name ] = true;

			if ( ! isset( $relational[ $field_key ] ) ) {
				$field                    = acf_get_field( $field_key );
				$relational[ $field_key ] = $field && in_array( $field['type'], array( 'relationship', 'post_object', 'page_link' ), true );
			}

			if ( ! $relational[ $field_key ] ) {
				continue;
			}

			foreach ( acf_get_array( acf_maybe_unserialize( $value ) ) as $id ) {
				if ( is_numeric( $id ) ) {
					$snapshot['ids'][ $field_key ][ (string) $id ] = true;
				}
			}
		}

		$snapshots[ $post_id ] = $snapshot;
		acf_set_data( 'acf_stored_post_ids_snapshots', $snapshots );
	}

	return $snapshots[ $post_id ];
}