update_metadata( string $meta_type, int $object_id, string $meta_key, mixed $meta_value, mixed $prev_value = '' )
Met à jour la méta-donnée d'un objet donné. Si la méta-donnée n'existe pas pour cet objet, elle sera ajoutée.
Paramètres
$meta_type
(string)requisType d'objet. Accepte 'post', 'comment', 'term', 'user' ou tout autre type d'objet associé à une table de méta-donnée.
$object_id
(int)requisId de l'objet.
$meta_key
(string)requisClé de la méta-donnée.
$meta_value
(mixed)requisValeur de la méta-donnée. Doit être sérializable si non scalaire.
$prev_value
(mixed)optionnelValeur précédente à vérifier avant la mise à jour. Si spécifiée, met seulement à jour la méta-donné correspondant à cette valeur.
Valeur par défaut : ''
Retourne
(int|bool) Id de la nouvelle méta-donnée ajoutée si celle-ci n'existait pas, true si la mise à jour est un succès, false si échec ou si $meta_value à la même valeur que celle en base de donnée.
Déclaration et structure de la fonction update_metadata()
update_metadata()
est déclarée dans le fichier wp-includes/meta.php
à la ligne 182 :
function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_value = '' ) {
global $wpdb;
if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
return false;
}
$object_id = absint( $object_id );
if ( ! $object_id ) {
return false;
}
$table = _get_meta_table( $meta_type );
if ( ! $table ) {
return false;
}
$meta_subtype = get_object_subtype( $meta_type, $object_id );
$column = sanitize_key( $meta_type . '_id' );
$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
// expected_slashed ($meta_key)
$raw_meta_key = $meta_key;
$meta_key = wp_unslash( $meta_key );
$passed_value = $meta_value;
$meta_value = wp_unslash( $meta_value );
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
/**
* Short-circuits updating metadata of a specific type.
*
* 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`
* - `update_comment_metadata`
* - `update_term_metadata`
* - `update_user_metadata`
*
* @since 3.1.0
*
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param mixed $prev_value Optional. Previous value to check before updating.
* If specified, only update existing metadata entries with
* this value. Otherwise, update all entries.
*/
$check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );
if ( null !== $check ) {
return (bool) $check;
}
// Compare existing value to new value if no prev value given and the key exists only once.
if ( empty( $prev_value ) ) {
$old_value = get_metadata_raw( $meta_type, $object_id, $meta_key );
if ( is_countable( $old_value ) && count( $old_value ) === 1 ) {
if ( $old_value[0] === $meta_value ) {
return false;
}
}
}
$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) );
if ( empty( $meta_ids ) ) {
return add_metadata( $meta_type, $object_id, $raw_meta_key, $passed_value );
}
$_meta_value = $meta_value;
$meta_value = maybe_serialize( $meta_value );
$data = compact( 'meta_value' );
$where = array(
$column => $object_id,
'meta_key' => $meta_key,
);
if ( ! empty( $prev_value ) ) {
$prev_value = maybe_serialize( $prev_value );
$where['meta_value'] = $prev_value;
}
foreach ( $meta_ids as $meta_id ) {
/**
* Fires immediately before updating metadata of a specific type.
*
* 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).
*
* Possible hook names include:
*
* - `update_post_meta`
* - `update_comment_meta`
* - `update_term_meta`
* - `update_user_meta`
*
* @since 2.9.0
*
* @param int $meta_id ID of the metadata entry to update.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $_meta_value Metadata value.
*/
do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
if ( 'post' === $meta_type ) {
/**
* Fires immediately before updating a post's metadata.
*
* @since 2.9.0
*
* @param int $meta_id ID of metadata entry to update.
* @param int $object_id Post ID.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. This will be a PHP-serialized string representation of the value
* if the value is an array, an object, or itself a PHP-serialized string.
*/
do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
}
}
$result = $wpdb->update( $table, $data, $where );
if ( ! $result ) {
return false;
}
wp_cache_delete( $object_id, $meta_type . '_meta' );
foreach ( $meta_ids as $meta_id ) {
/**
* Fires immediately after updating metadata of a specific type.
*
* 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).
*
* Possible hook names include:
*
* - `updated_post_meta`
* - `updated_comment_meta`
* - `updated_term_meta`
* - `updated_user_meta`
*
* @since 2.9.0
*
* @param int $meta_id ID of updated metadata entry.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $_meta_value Metadata value.
*/
do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
if ( 'post' === $meta_type ) {
/**
* Fires immediately after updating a post's metadata.
*
* @since 2.9.0
*
* @param int $meta_id ID of updated metadata entry.
* @param int $object_id Post ID.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. This will be a PHP-serialized string representation of the value
* if the value is an array, an object, or itself a PHP-serialized string.
*/
do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
}
}
return true;
}
Fonctions utilisées par update_metadata()
get_metadata_raw()
Retourne la valeur originale de la méta-donnée pour une clé de méta et un id d'objet spécifiés.
get_object_subtype()
Retourne le sous-type d'un objet pour un type d'objet et un id d'objet donné.
is_countable()
Vérifie si le contenu de la variable est une valeur dénombrable.
wp_cache_delete()
Supprime les contenus du cache correspondant à la clé et au groupe.
wp_unslash()
Supprime les slashes d'une chaîne ou d'un tableau de chaînes.
sanitize_key()
Nettoie une clé de type chaîne.
absint()
Convertit une valeur en entier positif (valeur absolue).
maybe_serialize()
Sérialize une donnée si besoin.
apply_filters()
Appel les fonctions qui ont été attaché à un filtre (hook).
do_action()
Exécute des fonctions attachées à un hook spécifique.
_get_meta_table()
Retourne le nom d'une table de méta-donnée pour un type d'objet spécifié.
sanitize_meta()
Nettoie une valeur de méta-donnée.
add_metadata()
Ajoute une méta-donnée pour un objet spécifié.
Hooks utilisés par update_metadata()
update_meta_type_metadata
Court-circuite la méta-donnée d'un type spécifique à mettre à jour.
update_meta_type_meta
Se lance immédiatement avant que la méta-donnée d'un type spécifique soit mise à jour.
update_postmeta
Se lance immédiatement avant que la méta-donnée d'un poste soit mise à jour.
updated_meta_type_meta
Se lance immédiatement après que la méta-donnée d'un type spécifique soit mise à jour.
updated_postmeta
Se lance immédiatement après que la méta-donnée d'un poste soit mise à jour.
Où est utilisée la fonction update_metadata()
dans le CMS WordPress
Sources
Codex WordPress : update_metadata()
Autres fonctions dans le même fichier : wp-includes/meta.php