acf_rest_reduce_reference()
All-or-nothing reduction for reference field values.
If every referenced target passes acf_rest_reference_is_exposable(), the fully-expanded formatted value is returned unchanged. As soon as any one target fails the check, the whole field value is reduced to its light-format shape (integer ID for single-value fields, integer ID array for multi-value fields). This matches the ACF-1639 precedent for User fields and gives consumers a uniform response shape: they see either the full expansion or the compact ID shape they would have got from ?acf_format=light, not a mixed array.
IDs are taken from the stored raw value (which is always the resource ID or IDs for these field types), with a defensive fallback that extracts IDs from the formatted value if the raw value is unusable.
Хуков нет.
Возвращает
mixed.
Использование
acf_rest_reduce_reference( $formatted_value, $raw_value, $field );
- $formatted_value(разное) (обязательный)
- The formatted field value.
- $raw_value(разное) (обязательный)
- The raw field value (stored ID or ID array).
- $field(массив) (обязательный)
- The field array.
Список изменений
| С версии 6.8.10 | Введена. |
Код acf_rest_reduce_reference() acf rest reduce reference ACF 6.8.11
function acf_rest_reduce_reference( $formatted_value, $raw_value, $field ) {
if ( empty( $formatted_value ) ) {
return $formatted_value;
}
// Prefer raw_value — for reducible reference types it is always the
// stored resource ID (scalar) or IDs (array).
if ( is_array( $raw_value ) ) {
$item_ids = array_values( array_filter( array_map( 'intval', $raw_value ) ) );
$is_multi = true;
} elseif ( is_numeric( $raw_value ) && (int) $raw_value > 0 ) {
$item_ids = array( (int) $raw_value );
$is_multi = false;
} else {
// Defensive fallback — extract IDs from the formatted value when the
// raw value is missing or in an unexpected shape.
$is_multi = is_array( $formatted_value ) && ( empty( $formatted_value ) || acf_is_sequential_array( $formatted_value ) );
$iterable = $is_multi ? $formatted_value : array( $formatted_value );
$item_ids = array();
foreach ( (array) $iterable as $item ) {
if ( $item instanceof WP_Post ) {
$item_ids[] = (int) $item->ID;
} elseif ( is_array( $item ) && isset( $item['ID'] ) ) {
$item_ids[] = (int) $item['ID'];
} elseif ( is_numeric( $item ) ) {
$item_ids[] = (int) $item;
}
}
$item_ids = array_values( array_filter( $item_ids ) );
}
if ( empty( $item_ids ) ) {
return $formatted_value;
}
// All-or-nothing: any single unauthorised target collapses the whole field.
foreach ( $item_ids as $id ) {
if ( ! acf_rest_reference_is_exposable( $id, $field ) ) {
return $is_multi ? $item_ids : (int) $item_ids[0];
}
}
return $formatted_value;
}