acf_get_metadata()
Retrieves specific metadata from the database.
Hooks from the function
Returns
Mixed.
Usage
acf_get_metadata( $post_id, $name, $hidden );
- $post_id(int|string)
- The post id.
- $name(string)
- The meta name.
Default:'' - $hidden(true|false)
- If the meta is hidden (starts with an underscore).
Default:false
Changelog
| Since 5.2.3 | Introduced. |
acf_get_metadata() acf get metadata code ACF 6.8.8
function acf_get_metadata( $post_id = 0, $name = '', $hidden = false ) {
// Allow filter to short-circuit logic.
$null = apply_filters( 'acf/pre_load_metadata', null, $post_id, $name, $hidden );
if ( $null !== null ) {
return ( $null === '__return_null' ) ? null : $null;
}
// Decode $post_id for $type and $id.
$decoded = acf_decode_post_id( $post_id );
$id = $decoded['id'];
$type = $decoded['type'];
// Hidden meta uses an underscore prefix.
$prefix = $hidden ? '_' : '';
// Bail early if no $id (possible during new acf_form).
if ( ! $id ) {
return null;
}
// Determine CRUD function.
// - Relies on decoded post_id result to identify option or meta types.
// - Uses xxx_metadata(type) instead of xxx_type_meta() to bypass additional logic that could alter the ID.
if ( $type === 'option' ) {
return get_option( "{$prefix}{$id}_{$name}", null );
} else {
$meta = get_metadata( $type, $id, "{$prefix}{$name}", false );
return isset( $meta[0] ) ? $meta[0] : null;
}
}