acf_update_metadata()
Updates metadata in the database.
Hooks from the function
Returns
Int|true|false. Meta ID if the key didn't exist, true on successful update, false on failure.
Usage
acf_update_metadata( $post_id, $name, $value, $hidden );
- $post_id(int|string)
- The post id.
- $name(string)
- The meta name.
Default:'' - $value(mixed)
- The meta value.
Default:'' - $hidden(true|false)
- If the meta is hidden (starts with an underscore).
Default:false
Changelog
| Since 5.2.3 | Introduced. |
acf_update_metadata() acf update metadata code ACF 6.0.4
function acf_update_metadata( $post_id = 0, $name = '', $value = '', $hidden = false ) {
// Allow filter to short-circuit logic.
$pre = apply_filters( 'acf/pre_update_metadata', null, $post_id, $name, $value, $hidden );
if ( $pre !== null ) {
return $pre;
}
// 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 false;
}
// 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' ) {
$value = wp_unslash( $value );
$autoload = (bool) acf_get_setting( 'autoload' );
return update_option( "{$prefix}{$id}_{$name}", $value, $autoload );
} else {
return update_metadata( $type, $id, "{$prefix}{$name}", $value );
}
}