metadata_exists( string $meta_type, int $object_id, string $meta_key )
Détermine si une méta-donnée existe en donnant sa clé et pour un poste donné.
Paramètres
$meta_type
(string)requisType de l'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.
Retourne
(bool) True si la méta-donnée existe.
Déclaration et structure de la fonction metadata_exists()
metadata_exists()
est déclarée dans le fichier wp-includes/meta.php
à la ligne 737 :
function metadata_exists( $meta_type, $object_id, $meta_key ) {
if ( ! $meta_type || ! is_numeric( $object_id ) ) {
return false;
}
$object_id = absint( $object_id );
if ( ! $object_id ) {
return false;
}
/** This filter is documented in wp-includes/meta.php */
$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true, $meta_type );
if ( null !== $check ) {
return (bool) $check;
}
$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
if ( ! $meta_cache ) {
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
$meta_cache = $meta_cache[ $object_id ];
}
if ( isset( $meta_cache[ $meta_key ] ) ) {
return true;
}
return false;
}
Fonctions utilisées par metadata_exists()
wp_cache_get()
Retourne les contenus du cache en donnant la clé et le groupe.
absint()
Convertit une valeur en entier positif (valeur absolue).
apply_filters()
Appel les fonctions qui ont été attaché à un filtre (hook).
update_meta_cache()
Met à jour le cache de la méta-donnée pour des objets spécifiés.
Hook utilisé par metadata_exists()
get_meta_type_metadata
Court-circuite la valeur retournée de la méta-donnée.
Où est utilisée la fonction metadata_exists()
dans le CMS WordPress
Exemples
if ( metadata_exists( 'term', $term_id, '_meta_key' ) ) {
$meta_value = get_term_meta( $term_id, '_meta_key', true );
}
if ( metadata_exists( 'post', $post_id, '_meta_key' ) ) {
$meta_value = get_post_meta( $post_id, '_meta_key', true );
}
if ( metadata_exists( 'user', $user_id, '_meta_key' ) ) {
$meta_value = get_user_meta( $user_id, '_meta_key', true );
}
Sources
Codex WordPress : metadata_exists()
Autres fonctions dans le même fichier : wp-includes/meta.php