acf_get_metadata_by_field()
Gets metadata from the database.
Hooks from the function
Returns
Mixed.
Usage
acf_get_metadata_by_field( $post_id, $field, $hidden );
- $post_id(int|string)
- The post id.
- $field(array)
- The field array.
Default:array() - $hidden(true|false)
- True if we should return the reference key.
Default:false
Changelog
| Since 6.4 | Introduced. |
acf_get_metadata_by_field() acf get metadata by field code ACF 6.8.8
function acf_get_metadata_by_field( $post_id = 0, $field = array(), bool $hidden = false ) {
if ( empty( $field['name'] ) ) {
return null;
}
// Allow filter to short-circuit logic.
$null = apply_filters( 'acf/pre_load_metadata', null, $post_id, $field['name'], $hidden );
if ( $null !== null ) {
return ( $null === '__return_null' ) ? null : $null;
}
// 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 null;
}
$meta_instance = acf_get_meta_instance( $type );
if ( ! $meta_instance ) {
return false;
}
if ( $hidden ) {
return $meta_instance->get_reference( $id, $field['name'] );
}
return $meta_instance->get_value( $id, $field );
}