Paramètres

$term(int|string)requis

Le terme à vérifier. Accepte l'id, le nom ou le slug du terme.

$taxonomy(string)optionnel

Le nom de la taxonomie à utiliser.

Valeur par défaut : ''

$parent(int)optionnel

Id du terme parent.

Valeur par défaut : null

Retourne

(mixed) Retourne null si le terme n'existe pas. Retourne l'id du terme si aucune taxonomie n'est spécifée et si le terme existe. Retourne un tableau de l'id du terme et l'id de la taxonomie du terme si la taxonomie est spécifiée et son terme existe. Retourne 0 si l'id du terme passé à la fonction vaut 0.

Déclaration et structure de la fonction term_exists()

function term_exists( $term, $taxonomy = '', $parent_term = null ) {
    global $_wp_suspend_cache_invalidation;

    if ( null === $term ) {
        return null;
    }

    $defaults = array(
        'get'                    => 'all',
        'fields'                 => 'ids',
        'number'                 => 1,
        'update_term_meta_cache' => false,
        'order'                  => 'ASC',
        'orderby'                => 'term_id',
        'suppress_filter'        => true,
    );

    // Ensure that while importing, queries are not cached.
    if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
        $defaults['cache_results'] = false;
    }

    if ( ! empty( $taxonomy ) ) {
        $defaults['taxonomy'] = $taxonomy;
        $defaults['fields']   = 'all';
    }

    /**
     * Filters default query arguments for checking if a term exists.
     *
     * @since 6.0.0
     *
     * @param array      $defaults    An array of arguments passed to get_terms().
     * @param int|string $term        The term to check. Accepts term ID, slug, or name.
     * @param string     $taxonomy    The taxonomy name to use. An empty string indicates
     *                                the search is against all taxonomies.
     * @param int|null   $parent_term ID of parent term under which to confine the exists search.
     *                                Null indicates the search is unconfined.
     */
    $defaults = apply_filters( 'term_exists_default_query_args', $defaults, $term, $taxonomy, $parent_term );

    if ( is_int( $term ) ) {
        if ( 0 === $term ) {
            return 0;
        }
        $args  = wp_parse_args( array( 'include' => array( $term ) ), $defaults );
        $terms = get_terms( $args );
    } else {
        $term = trim( wp_unslash( $term ) );
        if ( '' === $term ) {
            return null;
        }

        if ( ! empty( $taxonomy ) && is_numeric( $parent_term ) ) {
            $defaults['parent'] = (int) $parent_term;
        }

        $args  = wp_parse_args( array( 'slug' => sanitize_title( $term ) ), $defaults );
        $terms = get_terms( $args );
        if ( empty( $terms ) || is_wp_error( $terms ) ) {
            $args  = wp_parse_args( array( 'name' => $term ), $defaults );
            $terms = get_terms( $args );
        }
    }

    if ( empty( $terms ) || is_wp_error( $terms ) ) {
        return null;
    }

    $_term = array_shift( $terms );

    if ( ! empty( $taxonomy ) ) {
        return array(
            'term_id'          => (string) $_term->term_id,
            'term_taxonomy_id' => (string) $_term->term_taxonomy_id,
        );
    }

    return (string) $_term;
}
wp_unslash()

Supprime les slashes d'une chaîne ou d'un tableau de chaînes.

sanitize_title()

Nettoie et transforme une chaîne en slug qui peut être utilisé dans une url ou comme valeur d'attribut HTML.

Où est utilisée la fonction term_exists() dans le CMS WordPress

Exemple

$term = term_exists( 'animaux', 'category' );
if ( $term !== 0 && $term !== null ) {
    echo 'La categorie existe';
}

$term = term_exists( 'chiens', 'post_tag' );
if ( $term !== 0 && $term !== null ) {
    echo 'L'étiquette existe';
}

Sources

Codex WordPress : term_exists()

Autres fonctions dans le même fichier : wp-includes/taxonomy.php

Retour