is_object_in_term( int $object_id, string $taxonomy, int|string|array $terms = null )
Détermine si l'objet donné est associé à un des termes donnés.
Paramètres
$object_id
(int)requisId de l'objet (ex : poste, lien, ...)
$taxonomy
(string)requisNom de la taxonomie.
$terms
(int|string|array)optionnelId, nom ou slug du/des terme(s) à vérifier ou un tableau de ceux-ci.
Valeur par défaut : null
Description / Informations supplémentaires
Si aucun terme n'est donné, la fonction vérifie si l'objet est associé à des termes dans une taxonomie donnée.
Retourne
(bool|WP_Error) WP_Error s'il y a une erreur d'entrée.
Déclaration et structure de la fonction is_object_in_term()
is_object_in_term()
est déclarée dans le fichier wp-includes/taxonomy.php
à la ligne 4864 :
function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
$object_id = (int) $object_id;
if ( ! $object_id ) {
return new WP_Error( 'invalid_object', __( 'Invalid object ID.' ) );
}
$object_terms = get_object_term_cache( $object_id, $taxonomy );
if ( false === $object_terms ) {
$object_terms = wp_get_object_terms( $object_id, $taxonomy, array( 'update_term_meta_cache' => false ) );
if ( is_wp_error( $object_terms ) ) {
return $object_terms;
}
wp_cache_set( $object_id, wp_list_pluck( $object_terms, 'term_id' ), "{$taxonomy}_relationships" );
}
if ( is_wp_error( $object_terms ) ) {
return $object_terms;
}
if ( empty( $object_terms ) ) {
return false;
}
if ( empty( $terms ) ) {
return ( ! empty( $object_terms ) );
}
$terms = (array) $terms;
$ints = array_filter( $terms, 'is_int' );
if ( $ints ) {
$strs = array_diff( $terms, $ints );
} else {
$strs =& $terms;
}
foreach ( $object_terms as $object_term ) {
// If term is an int, check against term_ids only.
if ( $ints && in_array( $object_term->term_id, $ints, true ) ) {
return true;
}
if ( $strs ) {
// Only check numeric strings against term_id, to avoid false matches due to type juggling.
$numeric_strs = array_map( 'intval', array_filter( $strs, 'is_numeric' ) );
if ( in_array( $object_term->term_id, $numeric_strs, true ) ) {
return true;
}
if ( in_array( $object_term->name, $strs, true ) ) {
return true;
}
if ( in_array( $object_term->slug, $strs, true ) ) {
return true;
}
}
}
return false;
}
Fonctions utilisées par is_object_in_term()
wp_cache_set()
Sauvegarde les données dans le cache.
__()
Retourne la traduction d'un texte.
wp_list_pluck()
Retire certain champ d'une liste d'objet.
get_object_term_cache()
Retourne les objets de termes du cache pour un id d'objet donné.
wp_get_object_terms()
Retourne les termes associés à des objets donnés, en fournissant la taxonomie.
is_wp_error()
Vérifie si la variable est une erreur WordPress.
Où est utilisée la fonction is_object_in_term()
dans le CMS WordPress
Exemple
if ( is_object_in_term( $post->ID, 'custom_taxonomy_name', 'term_name' ) ) :
echo 'YES';
else :
echo 'NO';
endif;
Sources
Codex WordPress : is_object_in_term()
Autres fonctions dans le même fichier : wp-includes/taxonomy.php