term_exists

The timeline below displays how wordpress function term_exists has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.4

/**
 * Determines whether a taxonomy term exists.
 *
 * Formerly is_term(), introduced in 2.3.0.
 *
 * For more information on this and similar theme functions, check out
 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 * Conditional Tags} article in the Theme Developer Handbook.
 *
 * @since 3.0.0
 * @since 6.0.0 Converted to use `get_terms()`.
 *
 * @global bool $_wp_suspend_cache_invalidation
 *
 * @param int|string $term        The term to check. Accepts term ID, slug, or name.
 * @param string     $taxonomy    Optional. The taxonomy name to use.
 * @param int        $parent_term Optional. ID of parent term under which to confine the exists search.
 * @return mixed Returns null if the term does not exist.
 *               Returns the term ID if no taxonomy is specified and the term ID exists.
 *               Returns an array of the term ID and the term taxonomy ID if the taxonomy is specified and the pairing exists.
 *               Returns 0 if term ID 0 is passed to the function.
 */
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;
}

WordPress Version: 6.2

/**
 * Determines whether a taxonomy term exists.
 *
 * Formerly is_term(), introduced in 2.3.0.
 *
 * For more information on this and similar theme functions, check out
 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 * Conditional Tags} article in the Theme Developer Handbook.
 *
 * @since 3.0.0
 * @since 6.0.0 Converted to use `get_terms()`.
 *
 * @global bool $_wp_suspend_cache_invalidation
 *
 * @param int|string $term        The term to check. Accepts term ID, slug, or name.
 * @param string     $taxonomy    Optional. The taxonomy name to use.
 * @param int        $parent_term Optional. ID of parent term under which to confine the exists search.
 * @return mixed Returns null if the term does not exist.
 *               Returns the term ID if no taxonomy is specified and the term ID exists.
 *               Returns an array of the term ID and the term taxonomy ID if the taxonomy is specified and the pairing exists.
 *               Returns 0 if term ID 0 is passed to the function.
 */
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)) {
        // @todo Disable caching once #52710 is merged.
        $defaults['cache_domain'] = microtime();
    }
    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;
}

WordPress Version: 6.1

/**
 * Determines whether a taxonomy term exists.
 *
 * Formerly is_term(), introduced in 2.3.0.
 *
 * For more information on this and similar theme functions, check out
 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 * Conditional Tags} article in the Theme Developer Handbook.
 *
 * @since 3.0.0
 * @since 6.0.0 Converted to use `get_terms()`.
 *
 * @global bool $_wp_suspend_cache_invalidation
 *
 * @param int|string $term     The term to check. Accepts term ID, slug, or name.
 * @param string     $taxonomy Optional. The taxonomy name to use.
 * @param int        $parent   Optional. ID of parent term under which to confine the exists search.
 * @return mixed Returns null if the term does not exist.
 *               Returns the term ID if no taxonomy is specified and the term ID exists.
 *               Returns an array of the term ID and the term taxonomy ID if the taxonomy is specified and the pairing exists.
 *               Returns 0 if term ID 0 is passed to the function.
 */
function term_exists($term, $taxonomy = '', $parent = 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)) {
        // @todo Disable caching once #52710 is merged.
        $defaults['cache_domain'] = microtime();
    }
    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   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);
    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)) {
            $defaults['parent'] = (int) $parent;
        }
        $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;
}

WordPress Version: 5.9

/**
 * Determines whether a taxonomy term exists.
 *
 * Formerly is_term(), introduced in 2.3.0.
 *
 * For more information on this and similar theme functions, check out
 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 * Conditional Tags} article in the Theme Developer Handbook.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|string $term     The term to check. Accepts term ID, slug, or name.
 * @param string     $taxonomy Optional. The taxonomy name to use.
 * @param int        $parent   Optional. ID of parent term under which to confine the exists search.
 * @return mixed Returns null if the term does not exist.
 *               Returns the term ID if no taxonomy is specified and the term ID exists.
 *               Returns an array of the term ID and the term taxonomy ID if the taxonomy is specified and the pairing exists.
 *               Returns 0 if term ID 0 is passed to the function.
 */
function term_exists($term, $taxonomy = '', $parent = null)
{
    global $wpdb;
    if (null === $term) {
        return null;
    }
    $select = "SELECT term_id FROM {$wpdb->terms} as t WHERE ";
    $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE ";
    if (is_int($term)) {
        if (0 === $term) {
            return 0;
        }
        $where = 't.term_id = %d';
        if (!empty($taxonomy)) {
            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
            return $wpdb->get_row($wpdb->prepare($tax_select . $where . ' AND tt.taxonomy = %s', $term, $taxonomy), ARRAY_A);
        } else {
            return $wpdb->get_var($wpdb->prepare($select . $where, $term));
        }
    }
    $term = trim(wp_unslash($term));
    $slug = sanitize_title($term);
    $where = 't.slug = %s';
    $else_where = 't.name = %s';
    $where_fields = array($slug);
    $else_where_fields = array($term);
    $orderby = 'ORDER BY t.term_id ASC';
    $limit = 'LIMIT 1';
    if (!empty($taxonomy)) {
        if (is_numeric($parent)) {
            $parent = (int) $parent;
            $where_fields[] = $parent;
            $else_where_fields[] = $parent;
            $where .= ' AND tt.parent = %d';
            $else_where .= ' AND tt.parent = %d';
        }
        $where_fields[] = $taxonomy;
        $else_where_fields[] = $taxonomy;
        $result = $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$where} AND tt.taxonomy = %s {$orderby} {$limit}", $where_fields), ARRAY_A);
        if ($result) {
            return $result;
        }
        return $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$else_where} AND tt.taxonomy = %s {$orderby} {$limit}", $else_where_fields), ARRAY_A);
    }
    // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
    $result = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$where} {$orderby} {$limit}", $where_fields));
    if ($result) {
        return $result;
    }
    // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
    return $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$else_where} {$orderby} {$limit}", $else_where_fields));
}

WordPress Version: 5.4

/**
 * Determines whether a taxonomy term exists.
 *
 * Formerly is_term(), introduced in 2.3.0.
 *
 * For more information on this and similar theme functions, check out
 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 * Conditional Tags} article in the Theme Developer Handbook.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|string $term     The term to check. Accepts term ID, slug, or name.
 * @param string     $taxonomy Optional. The taxonomy name to use.
 * @param int        $parent   Optional. ID of parent term under which to confine the exists search.
 * @return mixed Returns null if the term does not exist.
 *               Returns the term ID if no taxonomy is specified and the term ID exists.
 *               Returns an array of the term ID and the term taxonomy ID if the taxonomy is specified and the pairing exists.
 *               Returns 0 if term ID 0 is passed to the function.
 */
function term_exists($term, $taxonomy = '', $parent = null)
{
    global $wpdb;
    $select = "SELECT term_id FROM {$wpdb->terms} as t WHERE ";
    $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE ";
    if (is_int($term)) {
        if (0 === $term) {
            return 0;
        }
        $where = 't.term_id = %d';
        if (!empty($taxonomy)) {
            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
            return $wpdb->get_row($wpdb->prepare($tax_select . $where . ' AND tt.taxonomy = %s', $term, $taxonomy), ARRAY_A);
        } else {
            return $wpdb->get_var($wpdb->prepare($select . $where, $term));
        }
    }
    $term = trim(wp_unslash($term));
    $slug = sanitize_title($term);
    $where = 't.slug = %s';
    $else_where = 't.name = %s';
    $where_fields = array($slug);
    $else_where_fields = array($term);
    $orderby = 'ORDER BY t.term_id ASC';
    $limit = 'LIMIT 1';
    if (!empty($taxonomy)) {
        if (is_numeric($parent)) {
            $parent = (int) $parent;
            $where_fields[] = $parent;
            $else_where_fields[] = $parent;
            $where .= ' AND tt.parent = %d';
            $else_where .= ' AND tt.parent = %d';
        }
        $where_fields[] = $taxonomy;
        $else_where_fields[] = $taxonomy;
        $result = $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$where} AND tt.taxonomy = %s {$orderby} {$limit}", $where_fields), ARRAY_A);
        if ($result) {
            return $result;
        }
        return $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$else_where} AND tt.taxonomy = %s {$orderby} {$limit}", $else_where_fields), ARRAY_A);
    }
    // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
    $result = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$where} {$orderby} {$limit}", $where_fields));
    if ($result) {
        return $result;
    }
    // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
    return $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$else_where} {$orderby} {$limit}", $else_where_fields));
}

WordPress Version: 5.3

/**
 * Determines whether a term exists.
 *
 * Formerly is_term(), introduced in 2.3.0.
 *
 * For more information on this and similar theme functions, check out
 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 * Conditional Tags} article in the Theme Developer Handbook.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|string $term     The term to check. Accepts term ID, slug, or name.
 * @param string     $taxonomy Optional. The taxonomy name to use.
 * @param int        $parent   Optional. ID of parent term under which to confine the exists search.
 * @return mixed Returns null if the term does not exist. Returns the term ID
 *               if no taxonomy is specified and the term ID exists. Returns
 *               an array of the term ID and the term taxonomy ID if the taxonomy
 *               is specified and the pairing exists.
 */
function term_exists($term, $taxonomy = '', $parent = null)
{
    global $wpdb;
    $select = "SELECT term_id FROM {$wpdb->terms} as t WHERE ";
    $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE ";
    if (is_int($term)) {
        if (0 === $term) {
            return 0;
        }
        $where = 't.term_id = %d';
        if (!empty($taxonomy)) {
            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
            return $wpdb->get_row($wpdb->prepare($tax_select . $where . ' AND tt.taxonomy = %s', $term, $taxonomy), ARRAY_A);
        } else {
            return $wpdb->get_var($wpdb->prepare($select . $where, $term));
        }
    }
    $term = trim(wp_unslash($term));
    $slug = sanitize_title($term);
    $where = 't.slug = %s';
    $else_where = 't.name = %s';
    $where_fields = array($slug);
    $else_where_fields = array($term);
    $orderby = 'ORDER BY t.term_id ASC';
    $limit = 'LIMIT 1';
    if (!empty($taxonomy)) {
        if (is_numeric($parent)) {
            $parent = (int) $parent;
            $where_fields[] = $parent;
            $else_where_fields[] = $parent;
            $where .= ' AND tt.parent = %d';
            $else_where .= ' AND tt.parent = %d';
        }
        $where_fields[] = $taxonomy;
        $else_where_fields[] = $taxonomy;
        $result = $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$where} AND tt.taxonomy = %s {$orderby} {$limit}", $where_fields), ARRAY_A);
        if ($result) {
            return $result;
        }
        return $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$else_where} AND tt.taxonomy = %s {$orderby} {$limit}", $else_where_fields), ARRAY_A);
    }
    // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
    $result = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$where} {$orderby} {$limit}", $where_fields));
    if ($result) {
        return $result;
    }
    // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
    return $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$else_where} {$orderby} {$limit}", $else_where_fields));
}

WordPress Version: 5.1

/**
 * Determines whether a term exists.
 *
 * Formerly is_term(), introduced in 2.3.0.
 *
 * For more information on this and similar theme functions, check out
 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 * Conditional Tags} article in the Theme Developer Handbook.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|string $term     The term to check. Accepts term ID, slug, or name.
 * @param string     $taxonomy The taxonomy name to use
 * @param int        $parent   Optional. ID of parent term under which to confine the exists search.
 * @return mixed Returns null if the term does not exist. Returns the term ID
 *               if no taxonomy is specified and the term ID exists. Returns
 *               an array of the term ID and the term taxonomy ID if the taxonomy
 *               is specified and the pairing exists.
 */
function term_exists($term, $taxonomy = '', $parent = null)
{
    global $wpdb;
    $select = "SELECT term_id FROM {$wpdb->terms} as t WHERE ";
    $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE ";
    if (is_int($term)) {
        if (0 == $term) {
            return 0;
        }
        $where = 't.term_id = %d';
        if (!empty($taxonomy)) {
            return $wpdb->get_row($wpdb->prepare($tax_select . $where . ' AND tt.taxonomy = %s', $term, $taxonomy), ARRAY_A);
        } else {
            return $wpdb->get_var($wpdb->prepare($select . $where, $term));
        }
    }
    $term = trim(wp_unslash($term));
    $slug = sanitize_title($term);
    $where = 't.slug = %s';
    $else_where = 't.name = %s';
    $where_fields = array($slug);
    $else_where_fields = array($term);
    $orderby = 'ORDER BY t.term_id ASC';
    $limit = 'LIMIT 1';
    if (!empty($taxonomy)) {
        if (is_numeric($parent)) {
            $parent = (int) $parent;
            $where_fields[] = $parent;
            $else_where_fields[] = $parent;
            $where .= ' AND tt.parent = %d';
            $else_where .= ' AND tt.parent = %d';
        }
        $where_fields[] = $taxonomy;
        $else_where_fields[] = $taxonomy;
        if ($result = $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$where} AND tt.taxonomy = %s {$orderby} {$limit}", $where_fields), ARRAY_A)) {
            return $result;
        }
        return $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$else_where} AND tt.taxonomy = %s {$orderby} {$limit}", $else_where_fields), ARRAY_A);
    }
    if ($result = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$where} {$orderby} {$limit}", $where_fields))) {
        return $result;
    }
    return $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$else_where} {$orderby} {$limit}", $else_where_fields));
}

WordPress Version: 5.0

/**
 * Determines whether a term exists.
 *
 * Formerly is_term(), introduced in 2.3.0.
 * 
 * For more information on this and similar theme functions, check out
 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 
 * Conditional Tags} article in the Theme Developer Handbook.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|string $term     The term to check. Accepts term ID, slug, or name.
 * @param string     $taxonomy The taxonomy name to use
 * @param int        $parent   Optional. ID of parent term under which to confine the exists search.
 * @return mixed Returns null if the term does not exist. Returns the term ID
 *               if no taxonomy is specified and the term ID exists. Returns
 *               an array of the term ID and the term taxonomy ID the taxonomy
 *               is specified and the pairing exists.
 */
function term_exists($term, $taxonomy = '', $parent = null)
{
    global $wpdb;
    $select = "SELECT term_id FROM {$wpdb->terms} as t WHERE ";
    $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE ";
    if (is_int($term)) {
        if (0 == $term) {
            return 0;
        }
        $where = 't.term_id = %d';
        if (!empty($taxonomy)) {
            return $wpdb->get_row($wpdb->prepare($tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy), ARRAY_A);
        } else {
            return $wpdb->get_var($wpdb->prepare($select . $where, $term));
        }
    }
    $term = trim(wp_unslash($term));
    $slug = sanitize_title($term);
    $where = 't.slug = %s';
    $else_where = 't.name = %s';
    $where_fields = array($slug);
    $else_where_fields = array($term);
    $orderby = 'ORDER BY t.term_id ASC';
    $limit = 'LIMIT 1';
    if (!empty($taxonomy)) {
        if (is_numeric($parent)) {
            $parent = (int) $parent;
            $where_fields[] = $parent;
            $else_where_fields[] = $parent;
            $where .= ' AND tt.parent = %d';
            $else_where .= ' AND tt.parent = %d';
        }
        $where_fields[] = $taxonomy;
        $else_where_fields[] = $taxonomy;
        if ($result = $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$where} AND tt.taxonomy = %s {$orderby} {$limit}", $where_fields), ARRAY_A)) {
            return $result;
        }
        return $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$else_where} AND tt.taxonomy = %s {$orderby} {$limit}", $else_where_fields), ARRAY_A);
    }
    if ($result = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$where} {$orderby} {$limit}", $where_fields))) {
        return $result;
    }
    return $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$else_where} {$orderby} {$limit}", $else_where_fields));
}

WordPress Version: 4.7

/**
 * Check if Term exists.
 *
 * Formerly is_term(), introduced in 2.3.0.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|string $term     The term to check. Accepts term ID, slug, or name.
 * @param string     $taxonomy The taxonomy name to use
 * @param int        $parent   Optional. ID of parent term under which to confine the exists search.
 * @return mixed Returns null if the term does not exist. Returns the term ID
 *               if no taxonomy is specified and the term ID exists. Returns
 *               an array of the term ID and the term taxonomy ID the taxonomy
 *               is specified and the pairing exists.
 */
function term_exists($term, $taxonomy = '', $parent = null)
{
    global $wpdb;
    $select = "SELECT term_id FROM {$wpdb->terms} as t WHERE ";
    $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE ";
    if (is_int($term)) {
        if (0 == $term) {
            return 0;
        }
        $where = 't.term_id = %d';
        if (!empty($taxonomy)) {
            return $wpdb->get_row($wpdb->prepare($tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy), ARRAY_A);
        } else {
            return $wpdb->get_var($wpdb->prepare($select . $where, $term));
        }
    }
    $term = trim(wp_unslash($term));
    $slug = sanitize_title($term);
    $where = 't.slug = %s';
    $else_where = 't.name = %s';
    $where_fields = array($slug);
    $else_where_fields = array($term);
    $orderby = 'ORDER BY t.term_id ASC';
    $limit = 'LIMIT 1';
    if (!empty($taxonomy)) {
        if (is_numeric($parent)) {
            $parent = (int) $parent;
            $where_fields[] = $parent;
            $else_where_fields[] = $parent;
            $where .= ' AND tt.parent = %d';
            $else_where .= ' AND tt.parent = %d';
        }
        $where_fields[] = $taxonomy;
        $else_where_fields[] = $taxonomy;
        if ($result = $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$where} AND tt.taxonomy = %s {$orderby} {$limit}", $where_fields), ARRAY_A)) {
            return $result;
        }
        return $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$else_where} AND tt.taxonomy = %s {$orderby} {$limit}", $else_where_fields), ARRAY_A);
    }
    if ($result = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$where} {$orderby} {$limit}", $where_fields))) {
        return $result;
    }
    return $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$else_where} {$orderby} {$limit}", $else_where_fields));
}

WordPress Version: 4.3

/**
 * Check if Term exists.
 *
 * Formerly is_term(), introduced in 2.3.0.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|string $term     The term to check
 * @param string     $taxonomy The taxonomy name to use
 * @param int        $parent   Optional. ID of parent term under which to confine the exists search.
 * @return mixed Returns null if the term does not exist. Returns the term ID
 *               if no taxonomy is specified and the term ID exists. Returns
 *               an array of the term ID and the term taxonomy ID the taxonomy
 *               is specified and the pairing exists.
 */
function term_exists($term, $taxonomy = '', $parent = null)
{
    global $wpdb;
    $select = "SELECT term_id FROM {$wpdb->terms} as t WHERE ";
    $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE ";
    if (is_int($term)) {
        if (0 == $term) {
            return 0;
        }
        $where = 't.term_id = %d';
        if (!empty($taxonomy)) {
            return $wpdb->get_row($wpdb->prepare($tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy), ARRAY_A);
        } else {
            return $wpdb->get_var($wpdb->prepare($select . $where, $term));
        }
    }
    $term = trim(wp_unslash($term));
    $slug = sanitize_title($term);
    $where = 't.slug = %s';
    $else_where = 't.name = %s';
    $where_fields = array($slug);
    $else_where_fields = array($term);
    $orderby = 'ORDER BY t.term_id ASC';
    $limit = 'LIMIT 1';
    if (!empty($taxonomy)) {
        if (is_numeric($parent)) {
            $parent = (int) $parent;
            $where_fields[] = $parent;
            $else_where_fields[] = $parent;
            $where .= ' AND tt.parent = %d';
            $else_where .= ' AND tt.parent = %d';
        }
        $where_fields[] = $taxonomy;
        $else_where_fields[] = $taxonomy;
        if ($result = $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$where} AND tt.taxonomy = %s {$orderby} {$limit}", $where_fields), ARRAY_A)) {
            return $result;
        }
        return $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$else_where} AND tt.taxonomy = %s {$orderby} {$limit}", $else_where_fields), ARRAY_A);
    }
    if ($result = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$where} {$orderby} {$limit}", $where_fields))) {
        return $result;
    }
    return $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$else_where} {$orderby} {$limit}", $else_where_fields));
}

WordPress Version: 4.1

/**
 * Check if Term exists.
 *
 * Formerly is_term(), introduced in 2.3.0.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|string $term The term to check
 * @param string $taxonomy The taxonomy name to use
 * @param int $parent Optional. ID of parent term under which to confine the exists search.
 * @return mixed Returns null if the term does not exist. Returns the term ID
 *               if no taxonomy is specified and the term ID exists. Returns
 *               an array of the term ID and the term taxonomy ID the taxonomy
 *               is specified and the pairing exists.
 */
function term_exists($term, $taxonomy = '', $parent = null)
{
    global $wpdb;
    $select = "SELECT term_id FROM {$wpdb->terms} as t WHERE ";
    $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE ";
    if (is_int($term)) {
        if (0 == $term) {
            return 0;
        }
        $where = 't.term_id = %d';
        if (!empty($taxonomy)) {
            return $wpdb->get_row($wpdb->prepare($tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy), ARRAY_A);
        } else {
            return $wpdb->get_var($wpdb->prepare($select . $where, $term));
        }
    }
    $term = trim(wp_unslash($term));
    $slug = sanitize_title($term);
    $where = 't.slug = %s';
    $else_where = 't.name = %s';
    $where_fields = array($slug);
    $else_where_fields = array($term);
    $orderby = 'ORDER BY t.term_id ASC';
    $limit = 'LIMIT 1';
    if (!empty($taxonomy)) {
        if (is_numeric($parent)) {
            $parent = (int) $parent;
            $where_fields[] = $parent;
            $else_where_fields[] = $parent;
            $where .= ' AND tt.parent = %d';
            $else_where .= ' AND tt.parent = %d';
        }
        $where_fields[] = $taxonomy;
        $else_where_fields[] = $taxonomy;
        if ($result = $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$where} AND tt.taxonomy = %s {$orderby} {$limit}", $where_fields), ARRAY_A)) {
            return $result;
        }
        return $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$else_where} AND tt.taxonomy = %s {$orderby} {$limit}", $else_where_fields), ARRAY_A);
    }
    if ($result = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$where} {$orderby} {$limit}", $where_fields))) {
        return $result;
    }
    return $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$else_where} {$orderby} {$limit}", $else_where_fields));
}

WordPress Version: 4.0

/**
 * Check if Term exists.
 *
 * Formerly is_term(), introduced in 2.3.0.
 *
 * @since 3.0.0
 *
 * @uses $wpdb
 *
 * @param int|string $term The term to check
 * @param string $taxonomy The taxonomy name to use
 * @param int $parent ID of parent term under which to confine the exists search.
 * @return mixed Returns 0 if the term does not exist. Returns the term ID if no taxonomy is specified
 *               and the term ID exists. Returns an array of the term ID and the term taxonomy ID
 *               if the taxonomy is specified and the pairing exists.
 */
function term_exists($term, $taxonomy = '', $parent = 0)
{
    global $wpdb;
    $select = "SELECT term_id FROM {$wpdb->terms} as t WHERE ";
    $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE ";
    if (is_int($term)) {
        if (0 == $term) {
            return 0;
        }
        $where = 't.term_id = %d';
        if (!empty($taxonomy)) {
            return $wpdb->get_row($wpdb->prepare($tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy), ARRAY_A);
        } else {
            return $wpdb->get_var($wpdb->prepare($select . $where, $term));
        }
    }
    $term = trim(wp_unslash($term));
    if ('' === $slug = sanitize_title($term)) {
        return 0;
    }
    $where = 't.slug = %s';
    $else_where = 't.name = %s';
    $where_fields = array($slug);
    $else_where_fields = array($term);
    if (!empty($taxonomy)) {
        $parent = (int) $parent;
        if ($parent > 0) {
            $where_fields[] = $parent;
            $else_where_fields[] = $parent;
            $where .= ' AND tt.parent = %d';
            $else_where .= ' AND tt.parent = %d';
        }
        $where_fields[] = $taxonomy;
        $else_where_fields[] = $taxonomy;
        if ($result = $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$where} AND tt.taxonomy = %s", $where_fields), ARRAY_A)) {
            return $result;
        }
        return $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$else_where} AND tt.taxonomy = %s", $else_where_fields), ARRAY_A);
    }
    if ($result = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$where}", $where_fields))) {
        return $result;
    }
    return $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$else_where}", $else_where_fields));
}

WordPress Version: 3.9

/**
 * Check if Term exists.
 *
 * Formerly is_term(), introduced in 2.3.0.
 *
 * @since 3.0.0
 *
 * @uses $wpdb
 *
 * @param int|string $term The term to check
 * @param string $taxonomy The taxonomy name to use
 * @param int $parent ID of parent term under which to confine the exists search.
 * @return mixed Returns 0 if the term does not exist. Returns the term ID if no taxonomy is specified
 * 	and the term ID exists. Returns an array of the term ID and the taxonomy if the pairing exists.
 */
function term_exists($term, $taxonomy = '', $parent = 0)
{
    global $wpdb;
    $select = "SELECT term_id FROM {$wpdb->terms} as t WHERE ";
    $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE ";
    if (is_int($term)) {
        if (0 == $term) {
            return 0;
        }
        $where = 't.term_id = %d';
        if (!empty($taxonomy)) {
            return $wpdb->get_row($wpdb->prepare($tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy), ARRAY_A);
        } else {
            return $wpdb->get_var($wpdb->prepare($select . $where, $term));
        }
    }
    $term = trim(wp_unslash($term));
    if ('' === $slug = sanitize_title($term)) {
        return 0;
    }
    $where = 't.slug = %s';
    $else_where = 't.name = %s';
    $where_fields = array($slug);
    $else_where_fields = array($term);
    if (!empty($taxonomy)) {
        $parent = (int) $parent;
        if ($parent > 0) {
            $where_fields[] = $parent;
            $else_where_fields[] = $parent;
            $where .= ' AND tt.parent = %d';
            $else_where .= ' AND tt.parent = %d';
        }
        $where_fields[] = $taxonomy;
        $else_where_fields[] = $taxonomy;
        if ($result = $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$where} AND tt.taxonomy = %s", $where_fields), ARRAY_A)) {
            return $result;
        }
        return $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$else_where} AND tt.taxonomy = %s", $else_where_fields), ARRAY_A);
    }
    if ($result = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$where}", $where_fields))) {
        return $result;
    }
    return $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$else_where}", $else_where_fields));
}

WordPress Version: 3.7

/**
 * Check if Term exists.
 *
 * Formerly is_term(), introduced in 2.3.0.
 *
 * @package WordPress
 * @subpackage Taxonomy
 * @since 3.0.0
 *
 * @uses $wpdb
 *
 * @param int|string $term The term to check
 * @param string $taxonomy The taxonomy name to use
 * @param int $parent ID of parent term under which to confine the exists search.
 * @return mixed Returns 0 if the term does not exist. Returns the term ID if no taxonomy is specified
 * 	and the term ID exists. Returns an array of the term ID and the taxonomy if the pairing exists.
 */
function term_exists($term, $taxonomy = '', $parent = 0)
{
    global $wpdb;
    $select = "SELECT term_id FROM {$wpdb->terms} as t WHERE ";
    $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE ";
    if (is_int($term)) {
        if (0 == $term) {
            return 0;
        }
        $where = 't.term_id = %d';
        if (!empty($taxonomy)) {
            return $wpdb->get_row($wpdb->prepare($tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy), ARRAY_A);
        } else {
            return $wpdb->get_var($wpdb->prepare($select . $where, $term));
        }
    }
    $term = trim(wp_unslash($term));
    if ('' === $slug = sanitize_title($term)) {
        return 0;
    }
    $where = 't.slug = %s';
    $else_where = 't.name = %s';
    $where_fields = array($slug);
    $else_where_fields = array($term);
    if (!empty($taxonomy)) {
        $parent = (int) $parent;
        if ($parent > 0) {
            $where_fields[] = $parent;
            $else_where_fields[] = $parent;
            $where .= ' AND tt.parent = %d';
            $else_where .= ' AND tt.parent = %d';
        }
        $where_fields[] = $taxonomy;
        $else_where_fields[] = $taxonomy;
        if ($result = $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$where} AND tt.taxonomy = %s", $where_fields), ARRAY_A)) {
            return $result;
        }
        return $wpdb->get_row($wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE {$else_where} AND tt.taxonomy = %s", $else_where_fields), ARRAY_A);
    }
    if ($result = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$where}", $where_fields))) {
        return $result;
    }
    return $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} as t WHERE {$else_where}", $else_where_fields));
}