acf_get_value()
Retrieves the value for a given field and post_id.
Хуки из функции
Возвращает
Разное
.
Использование
acf_get_value( $post_id, $field );
- $post_id(int|строка) (обязательный)
- The post id.
- $field(массив) (обязательный)
- The field array.
Список изменений
С версии 5.0.0 | Введена. |
Код acf_get_value() acf get value ACF 6.4.2
function acf_get_value( $post_id, $field ) { // Allow filter to short-circuit load_value logic. $value = apply_filters( 'acf/pre_load_value', null, $post_id, $field ); if ( $value !== null ) { return $value; } // Get field name. $field_name = $field['name']; // Get field ID & type. $decoded = acf_decode_post_id( $post_id ); $allow_load = true; // If we don't have a proper field array, the field doesn't exist currently. if ( empty( $field['type'] ) && empty( $field['key'] ) ) { // Check if we should trigger warning about accessing fields too early via action. do_action( 'acf/get_invalid_field_value', $field, __FUNCTION__ ); if ( apply_filters( 'acf/prevent_access_to_unknown_fields', false ) || ( 'option' === $decoded['type'] && 'options' !== $decoded['id'] ) ) { $allow_load = false; } } // If we're using a non options_ option key, ensure we have a valid reference key. if ( 'option' === $decoded['type'] && 'options' !== $decoded['id'] ) { // TODO: Move this into options meta class? i.e. return false $meta = acf_get_metadata_by_field( $post_id, $field, true ); if ( ! $meta ) { $allow_load = false; } elseif ( $meta !== $field['key'] ) { if ( ! isset( $field['__key'] ) || $meta !== $field['__key'] ) { $allow_load = false; } } } // Load Store. $store = acf_get_store( 'values' ); // If we're allowing load, check the store or load value from database. if ( $allow_load ) { if ( $store->has( "$post_id:$field_name" ) ) { return $store->get( "$post_id:$field_name" ); } $value = acf_get_metadata_by_field( $post_id, $field ); } // Use field's default_value if no meta was found. if ( $value === null && isset( $field['default_value'] ) ) { $value = $field['default_value']; } /** * Filters the $value after it has been loaded. * * @date 28/09/13 * @since 5.0.0 * * @param mixed $value The value to preview. * @param string $post_id The post ID for this value. * @param array $field The field array. */ $value = apply_filters( 'acf/load_value', $value, $post_id, $field ); // Update store if we allowed the value load. if ( $allow_load ) { $store->set( "$post_id:$field_name", $value ); } // Return value. return $value; }