WordPress Version: 6.3
/**
* Updates metadata by meta ID.
*
* @since 3.3.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param int $meta_id ID for a specific meta row.
* @param string $meta_value Metadata value. Must be serializable if non-scalar.
* @param string|false $meta_key Optional. You can provide a meta key to update it. Default false.
* @return bool True on successful update, false on failure.
*/
function update_metadata_by_mid($meta_type, $meta_id, $meta_value, $meta_key = false)
{
global $wpdb;
// Make sure everything is valid.
if (!$meta_type || !is_numeric($meta_id) || floor($meta_id) != $meta_id) {
return false;
}
$meta_id = (int) $meta_id;
if ($meta_id <= 0) {
return false;
}
$table = _get_meta_table($meta_type);
if (!$table) {
return false;
}
$column = sanitize_key($meta_type . '_id');
$id_column = ('user' === $meta_type) ? 'umeta_id' : 'meta_id';
/**
* Short-circuits updating metadata of a specific type by meta ID.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
* Returning a non-null value will effectively short-circuit the function.
*
* Possible hook names include:
*
* - `update_post_metadata_by_mid`
* - `update_comment_metadata_by_mid`
* - `update_term_metadata_by_mid`
* - `update_user_metadata_by_mid`
*
* @since 5.0.0
*
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param int $meta_id Meta ID.
* @param mixed $meta_value Meta value. Must be serializable if non-scalar.
* @param string|false $meta_key Meta key, if provided.
*/
$check = apply_filters("update_{$meta_type}_metadata_by_mid", null, $meta_id, $meta_value, $meta_key);
if (null !== $check) {
return (bool) $check;
}
// Fetch the meta and go on if it's found.
$meta = get_metadata_by_mid($meta_type, $meta_id);
if ($meta) {
$original_key = $meta->meta_key;
$object_id = $meta->{$column};
/*
* If a new meta_key (last parameter) was specified, change the meta key,
* otherwise use the original key in the update statement.
*/
if (false === $meta_key) {
$meta_key = $original_key;
} elseif (!is_string($meta_key)) {
return false;
}
$meta_subtype = get_object_subtype($meta_type, $object_id);
// Sanitize the meta.
$_meta_value = $meta_value;
$meta_value = sanitize_meta($meta_key, $meta_value, $meta_type, $meta_subtype);
$meta_value = maybe_serialize($meta_value);
// Format the data query arguments.
$data = array('meta_key' => $meta_key, 'meta_value' => $meta_value);
// Format the where query arguments.
$where = array();
$where[$id_column] = $meta_id;
/** This action is documented in wp-includes/meta.php */
do_action("update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value);
if ('post' === $meta_type) {
/** This action is documented in wp-includes/meta.php */
do_action('update_postmeta', $meta_id, $object_id, $meta_key, $meta_value);
}
// Run the update query, all fields in $data are %s, $where is a %d.
$result = $wpdb->update($table, $data, $where, '%s', '%d');
if (!$result) {
return false;
}
// Clear the caches.
wp_cache_delete($object_id, $meta_type . '_meta');
/** This action is documented in wp-includes/meta.php */
do_action("updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value);
if ('post' === $meta_type) {
/** This action is documented in wp-includes/meta.php */
do_action('updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value);
}
return true;
}
// And if the meta was not found.
return false;
}