get_term_by

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

WordPress Version: 6.1

/**
 * Gets all term data from database by term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the term will be returned.
 *
 * This function will always return the first term that matches the `$field`-
 * `$value`-`$taxonomy` combination specified in the parameters. If your query
 * is likely to match more than one term (as is likely to be the case when
 * `$field` is 'name', for example), consider using get_terms() instead; that
 * way, you will get all matching terms, and can provide your own logic for
 * deciding which one was intended.
 *
 * @todo Better formatting for DocBlock.
 *
 * @since 2.3.0
 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
 *              a WP_Term object if `$output` is `OBJECT`.
 * @since 5.5.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
 *
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string     $field    Either 'slug', 'name', 'term_id' (or 'id', 'ID'), or 'term_taxonomy_id'.
 * @param string|int $value    Search for this term value.
 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
 * @param string     $output   Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
 *                             correspond to a WP_Term object, an associative array, or a numeric array,
 *                             respectively. Default OBJECT.
 * @param string     $filter   Optional. How to sanitize term fields. Default 'raw'.
 * @return WP_Term|array|false WP_Term instance (or array) on success, depending on the `$output` value.
 *                             False if `$taxonomy` does not exist or `$term` was not found.
 */
function get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw')
{
    // 'term_taxonomy_id' lookups don't require taxonomy checks.
    if ('term_taxonomy_id' !== $field && !taxonomy_exists($taxonomy)) {
        return false;
    }
    // No need to perform a query for empty 'slug' or 'name'.
    if ('slug' === $field || 'name' === $field) {
        $value = (string) $value;
        if (0 === strlen($value)) {
            return false;
        }
    }
    if ('id' === $field || 'ID' === $field || 'term_id' === $field) {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term) || null === $term) {
            $term = false;
        }
        return $term;
    }
    $args = array('get' => 'all', 'number' => 1, 'taxonomy' => $taxonomy, 'update_term_meta_cache' => false, 'orderby' => 'none', 'suppress_filter' => true);
    switch ($field) {
        case 'slug':
            $args['slug'] = $value;
            break;
        case 'name':
            $args['name'] = $value;
            break;
        case 'term_taxonomy_id':
            $args['term_taxonomy_id'] = $value;
            unset($args['taxonomy']);
            break;
        default:
            return false;
    }
    $terms = get_terms($args);
    if (is_wp_error($terms) || empty($terms)) {
        return false;
    }
    $term = array_shift($terms);
    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the DB.
    if ('term_taxonomy_id' === $field) {
        $taxonomy = $term->taxonomy;
    }
    return get_term($term, $taxonomy, $output, $filter);
}

WordPress Version: 5.9

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * This function will always return the first term that matches the `$field`-
 * `$value`-`$taxonomy` combination specified in the parameters. If your query
 * is likely to match more than one term (as is likely to be the case when
 * `$field` is 'name', for example), consider using get_terms() instead; that
 * way, you will get all matching terms, and can provide your own logic for
 * deciding which one was intended.
 *
 * @todo Better formatting for DocBlock.
 *
 * @since 2.3.0
 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
 *              a WP_Term object if `$output` is `OBJECT`.
 * @since 5.5.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
 *
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string     $field    Either 'slug', 'name', 'term_id' (or 'id', 'ID'), or 'term_taxonomy_id'.
 * @param string|int $value    Search for this term value.
 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
 * @param string     $output   Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
 *                             correspond to a WP_Term object, an associative array, or a numeric array,
 *                             respectively. Default OBJECT.
 * @param string     $filter   Optional. How to sanitize term fields. Default 'raw'.
 * @return WP_Term|array|false WP_Term instance (or array) on success, depending on the `$output` value.
 *                             False if `$taxonomy` does not exist or `$term` was not found.
 */
function get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw')
{
    // 'term_taxonomy_id' lookups don't require taxonomy checks.
    if ('term_taxonomy_id' !== $field && !taxonomy_exists($taxonomy)) {
        return false;
    }
    // No need to perform a query for empty 'slug' or 'name'.
    if ('slug' === $field || 'name' === $field) {
        $value = (string) $value;
        if (0 === strlen($value)) {
            return false;
        }
    }
    if ('id' === $field || 'ID' === $field || 'term_id' === $field) {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term) || null === $term) {
            $term = false;
        }
        return $term;
    }
    $args = array('get' => 'all', 'number' => 1, 'taxonomy' => $taxonomy, 'update_term_meta_cache' => false, 'orderby' => 'none', 'suppress_filter' => true);
    switch ($field) {
        case 'slug':
            $args['slug'] = $value;
            break;
        case 'name':
            $args['name'] = $value;
            break;
        case 'term_taxonomy_id':
            $args['term_taxonomy_id'] = $value;
            unset($args['taxonomy']);
            break;
        default:
            return false;
    }
    $terms = get_terms($args);
    if (is_wp_error($terms) || empty($terms)) {
        return false;
    }
    $term = array_shift($terms);
    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the DB.
    if ('term_taxonomy_id' === $field) {
        $taxonomy = $term->taxonomy;
    }
    return get_term($term, $taxonomy, $output, $filter);
}

WordPress Version: 5.5

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * This function will always return the first term that matches the `$field`-
 * `$value`-`$taxonomy` combination specified in the parameters. If your query
 * is likely to match more than one term (as is likely to be the case when
 * `$field` is 'name', for example), consider using get_terms() instead; that
 * way, you will get all matching terms, and can provide your own logic for
 * deciding which one was intended.
 *
 * @todo Better formatting for DocBlock.
 *
 * @since 2.3.0
 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
 *              a WP_Term object if `$output` is `OBJECT`.
 * @since 5.5.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
 *
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string     $field    Either 'slug', 'name', 'id' or 'ID' (term_id), or 'term_taxonomy_id'.
 * @param string|int $value    Search for this term value.
 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
 * @param string     $output   Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
 *                             correspond to a WP_Term object, an associative array, or a numeric array,
 *                             respectively. Default OBJECT.
 * @param string     $filter   Optional. How to sanitize term fields. Default 'raw'.
 * @return WP_Term|array|false WP_Term instance (or array) on success, depending on the `$output` value.
 *                             False if `$taxonomy` does not exist or `$term` was not found.
 */
function get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw')
{
    // 'term_taxonomy_id' lookups don't require taxonomy checks.
    if ('term_taxonomy_id' !== $field && !taxonomy_exists($taxonomy)) {
        return false;
    }
    // No need to perform a query for empty 'slug' or 'name'.
    if ('slug' === $field || 'name' === $field) {
        $value = (string) $value;
        if (0 === strlen($value)) {
            return false;
        }
    }
    if ('id' === $field || 'ID' === $field || 'term_id' === $field) {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term) || null === $term) {
            $term = false;
        }
        return $term;
    }
    $args = array('get' => 'all', 'number' => 1, 'taxonomy' => $taxonomy, 'update_term_meta_cache' => false, 'orderby' => 'none', 'suppress_filter' => true);
    switch ($field) {
        case 'slug':
            $args['slug'] = $value;
            break;
        case 'name':
            $args['name'] = $value;
            break;
        case 'term_taxonomy_id':
            $args['term_taxonomy_id'] = $value;
            unset($args['taxonomy']);
            break;
        default:
            return false;
    }
    $terms = get_terms($args);
    if (is_wp_error($terms) || empty($terms)) {
        return false;
    }
    $term = array_shift($terms);
    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the DB.
    if ('term_taxonomy_id' === $field) {
        $taxonomy = $term->taxonomy;
    }
    return get_term($term, $taxonomy, $output, $filter);
}

WordPress Version: 5.4

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * This function will always return the first term that matches the `$field`-
 * `$value`-`$taxonomy` combination specified in the parameters. If your query
 * is likely to match more than one term (as is likely to be the case when
 * `$field` is 'name', for example), consider using get_terms() instead; that
 * way, you will get all matching terms, and can provide your own logic for
 * deciding which one was intended.
 *
 * @todo Better formatting for DocBlock.
 *
 * @since 2.3.0
 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
 *              a WP_Term object if `$output` is `OBJECT`.
 *
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value    Search for this term value
 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
 * @param string     $output   Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
 *                             a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
 * @return WP_Term|array|false WP_Term instance (or array) on success. Will return false if `$taxonomy` does not exist
 *                             or `$term` was not found.
 */
function get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw')
{
    // 'term_taxonomy_id' lookups don't require taxonomy checks.
    if ('term_taxonomy_id' !== $field && !taxonomy_exists($taxonomy)) {
        return false;
    }
    // No need to perform a query for empty 'slug' or 'name'.
    if ('slug' === $field || 'name' === $field) {
        $value = (string) $value;
        if (0 === strlen($value)) {
            return false;
        }
    }
    if ('id' === $field || 'term_id' === $field) {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term) || null === $term) {
            $term = false;
        }
        return $term;
    }
    $args = array('get' => 'all', 'number' => 1, 'taxonomy' => $taxonomy, 'update_term_meta_cache' => false, 'orderby' => 'none', 'suppress_filter' => true);
    switch ($field) {
        case 'slug':
            $args['slug'] = $value;
            break;
        case 'name':
            $args['name'] = $value;
            break;
        case 'term_taxonomy_id':
            $args['term_taxonomy_id'] = $value;
            unset($args['taxonomy']);
            break;
        default:
            return false;
    }
    $terms = get_terms($args);
    if (is_wp_error($terms) || empty($terms)) {
        return false;
    }
    $term = array_shift($terms);
    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the DB.
    if ('term_taxonomy_id' === $field) {
        $taxonomy = $term->taxonomy;
    }
    return get_term($term, $taxonomy, $output, $filter);
}

WordPress Version: 4.9

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * This function will always return the first term that matches the `$field`-
 * `$value`-`$taxonomy` combination specified in the parameters. If your query
 * is likely to match more than one term (as is likely to be the case when
 * `$field` is 'name', for example), consider using get_terms() instead; that
 * way, you will get all matching terms, and can provide your own logic for
 * deciding which one was intended.
 *
 * @todo Better formatting for DocBlock.
 *
 * @since 2.3.0
 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
 *              a WP_Term object if `$output` is `OBJECT`.
 *
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value    Search for this term value
 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
 * @param string     $output   Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
 *                             a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
 * @return WP_Term|array|false WP_Term instance (or array) on success. Will return false if `$taxonomy` does not exist
 *                             or `$term` was not found.
 */
function get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw')
{
    // 'term_taxonomy_id' lookups don't require taxonomy checks.
    if ('term_taxonomy_id' !== $field && !taxonomy_exists($taxonomy)) {
        return false;
    }
    // No need to perform a query for empty 'slug' or 'name'.
    if ('slug' === $field || 'name' === $field) {
        $value = (string) $value;
        if (0 === strlen($value)) {
            return false;
        }
    }
    if ('id' === $field || 'term_id' === $field) {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term) || null === $term) {
            $term = false;
        }
        return $term;
    }
    $args = array('get' => 'all', 'number' => 1, 'taxonomy' => $taxonomy, 'update_term_meta_cache' => false, 'orderby' => 'none', 'suppress_filter' => true);
    switch ($field) {
        case 'slug':
            $args['slug'] = $value;
            break;
        case 'name':
            $args['name'] = $value;
            break;
        case 'term_taxonomy_id':
            $args['term_taxonomy_id'] = $value;
            unset($args['taxonomy']);
            break;
        default:
            return false;
    }
    $terms = get_terms($args);
    if (is_wp_error($terms) || empty($terms)) {
        return false;
    }
    $term = array_shift($terms);
    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db.
    if ('term_taxonomy_id' === $field) {
        $taxonomy = $term->taxonomy;
    }
    return get_term($term, $taxonomy, $output, $filter);
}

WordPress Version: 4.8

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * This function will always return the first term that matches the `$field`-
 * `$value`-`$taxonomy` combination specified in the parameters. If your query
 * is likely to match more than one term (as is likely to be the case when
 * `$field` is 'name', for example), consider using get_terms() instead; that
 * way, you will get all matching terms, and can provide your own logic for
 * deciding which one was intended.
 *
 * @todo Better formatting for DocBlock.
 *
 * @since 2.3.0
 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
 *              a WP_Term object if `$output` is `OBJECT`.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value    Search for this term value
 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
 * @param string     $output   Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
 *                             a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
 * @return WP_Term|array|false WP_Term instance (or array) on success. Will return false if `$taxonomy` does not exist
 *                             or `$term` was not found.
 */
function get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw')
{
    // 'term_taxonomy_id' lookups don't require taxonomy checks.
    if ('term_taxonomy_id' !== $field && !taxonomy_exists($taxonomy)) {
        return false;
    }
    // No need to perform a query for empty 'slug' or 'name'.
    if ('slug' === $field || 'name' === $field) {
        $value = (string) $value;
        if (0 === strlen($value)) {
            return false;
        }
    }
    if ('id' === $field || 'term_id' === $field) {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term) || null === $term) {
            $term = false;
        }
        return $term;
    }
    $args = array('get' => 'all', 'number' => 1, 'taxonomy' => $taxonomy, 'update_term_meta_cache' => false, 'orderby' => 'none', 'suppress_filter' => true);
    switch ($field) {
        case 'slug':
            $args['slug'] = $value;
            break;
        case 'name':
            $args['name'] = $value;
            break;
        case 'term_taxonomy_id':
            $args['term_taxonomy_id'] = $value;
            unset($args['taxonomy']);
            break;
        default:
            return false;
    }
    $terms = get_terms($args);
    if (is_wp_error($terms) || empty($terms)) {
        return false;
    }
    $term = array_shift($terms);
    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db.
    if ('term_taxonomy_id' === $field) {
        $taxonomy = $term->taxonomy;
    }
    return get_term($term, $taxonomy, $output, $filter);
}

WordPress Version: 4.7

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * This function will always return the first term that matches the `$field`-
 * `$value`-`$taxonomy` combination specified in the parameters. If your query
 * is likely to match more than one term (as is likely to be the case when
 * `$field` is 'name', for example), consider using get_terms() instead; that
 * way, you will get all matching terms, and can provide your own logic for
 * deciding which one was intended.
 *
 * @todo Better formatting for DocBlock.
 *
 * @since 2.3.0
 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
 *              a WP_Term object if `$output` is `OBJECT`.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value    Search for this term value
 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
 * @param string     $output   Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
 *                             a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
 * @return WP_Term|array|false WP_Term instance (or array) on success. Will return false if `$taxonomy` does not exist
 *                             or `$term` was not found.
 */
function get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw')
{
    global $wpdb;
    // 'term_taxonomy_id' lookups don't require taxonomy checks.
    if ('term_taxonomy_id' !== $field && !taxonomy_exists($taxonomy)) {
        return false;
    }
    $tax_clause = $wpdb->prepare("AND tt.taxonomy = %s", $taxonomy);
    if ('slug' == $field) {
        $_field = 't.slug';
        $value = sanitize_title($value);
        if (empty($value)) {
            return false;
        }
    } elseif ('name' == $field) {
        // Assume already escaped
        $value = wp_unslash($value);
        $_field = 't.name';
    } elseif ('term_taxonomy_id' == $field) {
        $value = (int) $value;
        $_field = 'tt.term_taxonomy_id';
        // No `taxonomy` clause when searching by 'term_taxonomy_id'.
        $tax_clause = '';
    } else {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term) || is_null($term)) {
            $term = false;
        }
        return $term;
    }
    $term = $wpdb->get_row($wpdb->prepare("SELECT t.*, tt.* FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE {$_field} = %s", $value) . " {$tax_clause} LIMIT 1");
    if (!$term) {
        return false;
    }
    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db.
    if ('term_taxonomy_id' === $field) {
        $taxonomy = $term->taxonomy;
    }
    wp_cache_add($term->term_id, $term, 'terms');
    return get_term($term, $taxonomy, $output, $filter);
}

WordPress Version: 4.6

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * This function will always return the first term that matches the `$field`-
 * `$value`-`$taxonomy` combination specified in the parameters. If your query
 * is likely to match more than one term (as is likely to be the case when
 * `$field` is 'name', for example), consider using get_terms() instead; that
 * way, you will get all matching terms, and can provide your own logic for
 * deciding which one was intended.
 *
 * @todo Better formatting for DocBlock.
 *
 * @since 2.3.0
 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
 *              a WP_Term object if `$output` is `OBJECT`.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value    Search for this term value
 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
 * @return WP_Term|bool WP_Term instance on success. Will return false if `$taxonomy` does not exist
 *                      or `$term` was not found.
 */
function get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw')
{
    global $wpdb;
    // 'term_taxonomy_id' lookups don't require taxonomy checks.
    if ('term_taxonomy_id' !== $field && !taxonomy_exists($taxonomy)) {
        return false;
    }
    $tax_clause = $wpdb->prepare("AND tt.taxonomy = %s", $taxonomy);
    if ('slug' == $field) {
        $_field = 't.slug';
        $value = sanitize_title($value);
        if (empty($value)) {
            return false;
        }
    } elseif ('name' == $field) {
        // Assume already escaped
        $value = wp_unslash($value);
        $_field = 't.name';
    } elseif ('term_taxonomy_id' == $field) {
        $value = (int) $value;
        $_field = 'tt.term_taxonomy_id';
        // No `taxonomy` clause when searching by 'term_taxonomy_id'.
        $tax_clause = '';
    } else {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term) || is_null($term)) {
            $term = false;
        }
        return $term;
    }
    $term = $wpdb->get_row($wpdb->prepare("SELECT t.*, tt.* FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE {$_field} = %s", $value) . " {$tax_clause} LIMIT 1");
    if (!$term) {
        return false;
    }
    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db.
    if ('term_taxonomy_id' === $field) {
        $taxonomy = $term->taxonomy;
    }
    wp_cache_add($term->term_id, $term, 'terms');
    return get_term($term, $taxonomy, $output, $filter);
}

WordPress Version: .20

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * @todo Better formatting for DocBlock.
 *
 * @since 2.3.0
 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
 *              a WP_Term object if `$output` is `OBJECT`.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value    Search for this term value
 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
 * @return WP_Term|bool WP_Term instance on success. Will return false if `$taxonomy` does not exist
 *                      or `$term` was not found.
 */
function get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw')
{
    global $wpdb;
    // 'term_taxonomy_id' lookups don't require taxonomy checks.
    if ('term_taxonomy_id' !== $field && !taxonomy_exists($taxonomy)) {
        return false;
    }
    $tax_clause = $wpdb->prepare("AND tt.taxonomy = %s", $taxonomy);
    if ('slug' == $field) {
        $_field = 't.slug';
        $value = sanitize_title($value);
        if (empty($value)) {
            return false;
        }
    } elseif ('name' == $field) {
        // Assume already escaped
        $value = wp_unslash($value);
        $_field = 't.name';
    } elseif ('term_taxonomy_id' == $field) {
        $value = (int) $value;
        $_field = 'tt.term_taxonomy_id';
        // No `taxonomy` clause when searching by 'term_taxonomy_id'.
        $tax_clause = '';
    } else {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term) || is_null($term)) {
            $term = false;
        }
        return $term;
    }
    $term = $wpdb->get_row($wpdb->prepare("SELECT t.*, tt.* FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE {$_field} = %s", $value) . " {$tax_clause} LIMIT 1");
    if (!$term) {
        return false;
    }
    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db.
    if ('term_taxonomy_id' === $field) {
        $taxonomy = $term->taxonomy;
    }
    wp_cache_add($term->term_id, $term, 'terms');
    return get_term($term, $taxonomy, $output, $filter);
}

WordPress Version: 4.2

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * @todo Better formatting for DocBlock.
 *
 * @since 2.3.0
 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
 *              a WP_Term object if `$output` is `OBJECT`.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value    Search for this term value
 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
 * @return WP_Term|bool WP_Term instance on success. Will return false if `$taxonomy` does not exist
 *                      or `$term` was not found.
 */
function get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw')
{
    global $wpdb;
    // 'term_taxonomy_id' lookups don't require taxonomy checks.
    if ('term_taxonomy_id' !== $field && !taxonomy_exists($taxonomy)) {
        return false;
    }
    $tax_clause = $wpdb->prepare("AND tt.taxonomy = %s", $taxonomy);
    if ('slug' == $field) {
        $_field = 't.slug';
        $value = sanitize_title($value);
        if (empty($value)) {
            return false;
        }
    } elseif ('name' == $field) {
        // Assume already escaped
        $value = wp_unslash($value);
        $_field = 't.name';
    } elseif ('term_taxonomy_id' == $field) {
        $value = (int) $value;
        $_field = 'tt.term_taxonomy_id';
        // No `taxonomy` clause when searching by 'term_taxonomy_id'.
        $tax_clause = '';
    } else {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term) || is_null($term)) {
            $term = false;
        }
        return $term;
    }
    $term = $wpdb->get_row($wpdb->prepare("SELECT t.*, tt.* FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE {$_field} = %s {$tax_clause} LIMIT 1", $value));
    if (!$term) {
        return false;
    }
    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db.
    if ('term_taxonomy_id' === $field) {
        $taxonomy = $term->taxonomy;
    }
    wp_cache_add($term->term_id, $term, 'terms');
    return get_term($term, $taxonomy, $output, $filter);
}

WordPress Version: .10

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * @todo Better formatting for DocBlock.
 *
 * @since 2.3.0
 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
 *              a WP_Term object if `$output` is `OBJECT`.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value    Search for this term value
 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
 * @return WP_Term|bool WP_Term instance on success. Will return false if `$taxonomy` does not exist
 *                      or `$term` was not found.
 */
function get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw')
{
    global $wpdb;
    // 'term_taxonomy_id' lookups don't require taxonomy checks.
    if ('term_taxonomy_id' !== $field && !taxonomy_exists($taxonomy)) {
        return false;
    }
    $tax_clause = $wpdb->prepare("AND tt.taxonomy = %s", $taxonomy);
    if ('slug' == $field) {
        $_field = 't.slug';
        $value = sanitize_title($value);
        if (empty($value)) {
            return false;
        }
    } elseif ('name' == $field) {
        // Assume already escaped
        $value = wp_unslash($value);
        $_field = 't.name';
    } elseif ('term_taxonomy_id' == $field) {
        $value = (int) $value;
        $_field = 'tt.term_taxonomy_id';
        // No `taxonomy` clause when searching by 'term_taxonomy_id'.
        $tax_clause = '';
    } else {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term) || is_null($term)) {
            $term = false;
        }
        return $term;
    }
    $term = $wpdb->get_row($wpdb->prepare("SELECT t.*, tt.* FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE {$_field} = %s", $value) . " {$tax_clause} LIMIT 1");
    if (!$term) {
        return false;
    }
    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db.
    if ('term_taxonomy_id' === $field) {
        $taxonomy = $term->taxonomy;
    }
    wp_cache_add($term->term_id, $term, 'terms');
    return get_term($term, $taxonomy, $output, $filter);
}

WordPress Version: 4.4

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * @todo Better formatting for DocBlock.
 *
 * @since 2.3.0
 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
 *              a WP_Term object if `$output` is `OBJECT`.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value    Search for this term value
 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
 * @return WP_Term|bool WP_Term instance on success. Will return false if `$taxonomy` does not exist
 *                      or `$term` was not found.
 */
function get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw')
{
    global $wpdb;
    // 'term_taxonomy_id' lookups don't require taxonomy checks.
    if ('term_taxonomy_id' !== $field && !taxonomy_exists($taxonomy)) {
        return false;
    }
    $tax_clause = $wpdb->prepare("AND tt.taxonomy = %s", $taxonomy);
    if ('slug' == $field) {
        $_field = 't.slug';
        $value = sanitize_title($value);
        if (empty($value)) {
            return false;
        }
    } elseif ('name' == $field) {
        // Assume already escaped
        $value = wp_unslash($value);
        $_field = 't.name';
    } elseif ('term_taxonomy_id' == $field) {
        $value = (int) $value;
        $_field = 'tt.term_taxonomy_id';
        // No `taxonomy` clause when searching by 'term_taxonomy_id'.
        $tax_clause = '';
    } else {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term) || is_null($term)) {
            $term = false;
        }
        return $term;
    }
    $term = $wpdb->get_row($wpdb->prepare("SELECT t.*, tt.* FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE {$_field} = %s {$tax_clause} LIMIT 1", $value));
    if (!$term) {
        return false;
    }
    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db.
    if ('term_taxonomy_id' === $field) {
        $taxonomy = $term->taxonomy;
    }
    wp_cache_add($term->term_id, $term, 'terms');
    return get_term($term, $taxonomy, $output, $filter);
}

WordPress Version: 4.3

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * @todo Better formatting for DocBlock.
 *
 * @since 2.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value    Search for this term value
 * @param string     $taxonomy Taxonomy Name
 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
 * @return object|array|null|WP_Error|false Term Row from database.
 *                                          Will return false if $taxonomy does not exist or $term was not found.
 */
function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw')
{
    global $wpdb;
    if (!taxonomy_exists($taxonomy)) {
        return false;
    }
    if ('slug' == $field) {
        $field = 't.slug';
        $value = sanitize_title($value);
        if (empty($value)) {
            return false;
        }
    } elseif ('name' == $field) {
        // Assume already escaped
        $value = wp_unslash($value);
        $field = 't.name';
    } elseif ('term_taxonomy_id' == $field) {
        $value = (int) $value;
        $field = 'tt.term_taxonomy_id';
    } else {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term)) {
            $term = false;
        }
        return $term;
    }
    $term = $wpdb->get_row($wpdb->prepare("SELECT t.*, tt.* FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND {$field} = %s LIMIT 1", $taxonomy, $value));
    if (!$term) {
        return false;
    }
    wp_cache_add($term->term_id, $term, $taxonomy);
    /** This filter is documented in wp-includes/taxonomy.php */
    $term = apply_filters('get_term', $term, $taxonomy);
    /** This filter is documented in wp-includes/taxonomy.php */
    $term = apply_filters("get_{$taxonomy}", $term, $taxonomy);
    $term = sanitize_term($term, $taxonomy, $filter);
    if ($output == OBJECT) {
        return $term;
    } elseif ($output == ARRAY_A) {
        return get_object_vars($term);
    } elseif ($output == ARRAY_N) {
        return array_values(get_object_vars($term));
    } else {
        return $term;
    }
}

WordPress Version: 4.2

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * @since 2.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value Search for this term value
 * @param string $taxonomy Taxonomy Name
 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
 * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.
 */
function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw')
{
    global $wpdb;
    if (!taxonomy_exists($taxonomy)) {
        return false;
    }
    if ('slug' == $field) {
        $field = 't.slug';
        $value = sanitize_title($value);
        if (empty($value)) {
            return false;
        }
    } elseif ('name' == $field) {
        // Assume already escaped
        $value = wp_unslash($value);
        $field = 't.name';
    } elseif ('term_taxonomy_id' == $field) {
        $value = (int) $value;
        $field = 'tt.term_taxonomy_id';
    } else {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term)) {
            $term = false;
        }
        return $term;
    }
    $term = $wpdb->get_row($wpdb->prepare("SELECT t.*, tt.* FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND {$field} = %s LIMIT 1", $taxonomy, $value));
    if (!$term) {
        return false;
    }
    wp_cache_add($term->term_id, $term, $taxonomy);
    /** This filter is documented in wp-includes/taxonomy.php */
    $term = apply_filters('get_term', $term, $taxonomy);
    /** This filter is documented in wp-includes/taxonomy.php */
    $term = apply_filters("get_{$taxonomy}", $term, $taxonomy);
    $term = sanitize_term($term, $taxonomy, $filter);
    if ($output == OBJECT) {
        return $term;
    } elseif ($output == ARRAY_A) {
        return get_object_vars($term);
    } elseif ($output == ARRAY_N) {
        return array_values(get_object_vars($term));
    } else {
        return $term;
    }
}

WordPress Version: 4.1

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * @since 2.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value Search for this term value
 * @param string $taxonomy Taxonomy Name
 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
 * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.
 */
function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw')
{
    global $wpdb;
    if (!taxonomy_exists($taxonomy)) {
        return false;
    }
    if ('slug' == $field) {
        $field = 't.slug';
        $value = sanitize_title($value);
        if (empty($value)) {
            return false;
        }
    } else if ('name' == $field) {
        // Assume already escaped
        $value = wp_unslash($value);
        $field = 't.name';
    } else if ('term_taxonomy_id' == $field) {
        $value = (int) $value;
        $field = 'tt.term_taxonomy_id';
    } else {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term)) {
            $term = false;
        }
        return $term;
    }
    $term = $wpdb->get_row($wpdb->prepare("SELECT t.*, tt.* FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND {$field} = %s LIMIT 1", $taxonomy, $value));
    if (!$term) {
        return false;
    }
    wp_cache_add($term->term_id, $term, $taxonomy);
    /** This filter is documented in wp-includes/taxonomy.php */
    $term = apply_filters('get_term', $term, $taxonomy);
    /** This filter is documented in wp-includes/taxonomy.php */
    $term = apply_filters("get_{$taxonomy}", $term, $taxonomy);
    $term = sanitize_term($term, $taxonomy, $filter);
    if ($output == OBJECT) {
        return $term;
    } elseif ($output == ARRAY_A) {
        return get_object_vars($term);
    } elseif ($output == ARRAY_N) {
        return array_values(get_object_vars($term));
    } else {
        return $term;
    }
}

WordPress Version: 3.9

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * @since 2.3.0
 *
 * @uses $wpdb
 * @uses sanitize_term() Cleanses the term based on $filter context before returning.
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value Search for this term value
 * @param string $taxonomy Taxonomy Name
 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
 * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.
 */
function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw')
{
    global $wpdb;
    if (!taxonomy_exists($taxonomy)) {
        return false;
    }
    if ('slug' == $field) {
        $field = 't.slug';
        $value = sanitize_title($value);
        if (empty($value)) {
            return false;
        }
    } else if ('name' == $field) {
        // Assume already escaped
        $value = wp_unslash($value);
        $field = 't.name';
    } else if ('term_taxonomy_id' == $field) {
        $value = (int) $value;
        $field = 'tt.term_taxonomy_id';
    } else {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term)) {
            $term = false;
        }
        return $term;
    }
    $term = $wpdb->get_row($wpdb->prepare("SELECT t.*, tt.* FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND {$field} = %s LIMIT 1", $taxonomy, $value));
    if (!$term) {
        return false;
    }
    wp_cache_add($term->term_id, $term, $taxonomy);
    /** This filter is documented in wp-includes/taxonomy.php */
    $term = apply_filters('get_term', $term, $taxonomy);
    /** This filter is documented in wp-includes/taxonomy.php */
    $term = apply_filters("get_{$taxonomy}", $term, $taxonomy);
    $term = sanitize_term($term, $taxonomy, $filter);
    if ($output == OBJECT) {
        return $term;
    } elseif ($output == ARRAY_A) {
        return get_object_vars($term);
    } elseif ($output == ARRAY_N) {
        return array_values(get_object_vars($term));
    } else {
        return $term;
    }
}

WordPress Version: 3.7

/**
 * Get all Term data from database by Term field and data.
 *
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 * required.
 *
 * The default $field is 'id', therefore it is possible to also use null for
 * field, but not recommended that you do so.
 *
 * If $value does not exist, the return value will be false. If $taxonomy exists
 * and $field and $value combinations exist, the Term will be returned.
 *
 * @package WordPress
 * @subpackage Taxonomy
 * @since 2.3.0
 *
 * @uses $wpdb
 * @uses sanitize_term() Cleanses the term based on $filter context before returning.
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 *
 * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value Search for this term value
 * @param string $taxonomy Taxonomy Name
 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
 * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.
 */
function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw')
{
    global $wpdb;
    if (!taxonomy_exists($taxonomy)) {
        return false;
    }
    if ('slug' == $field) {
        $field = 't.slug';
        $value = sanitize_title($value);
        if (empty($value)) {
            return false;
        }
    } else if ('name' == $field) {
        // Assume already escaped
        $value = wp_unslash($value);
        $field = 't.name';
    } else if ('term_taxonomy_id' == $field) {
        $value = (int) $value;
        $field = 'tt.term_taxonomy_id';
    } else {
        $term = get_term((int) $value, $taxonomy, $output, $filter);
        if (is_wp_error($term)) {
            $term = false;
        }
        return $term;
    }
    $term = $wpdb->get_row($wpdb->prepare("SELECT t.*, tt.* FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND {$field} = %s LIMIT 1", $taxonomy, $value));
    if (!$term) {
        return false;
    }
    wp_cache_add($term->term_id, $term, $taxonomy);
    $term = apply_filters('get_term', $term, $taxonomy);
    $term = apply_filters("get_{$taxonomy}", $term, $taxonomy);
    $term = sanitize_term($term, $taxonomy, $filter);
    if ($output == OBJECT) {
        return $term;
    } elseif ($output == ARRAY_A) {
        return get_object_vars($term);
    } elseif ($output == ARRAY_N) {
        return array_values(get_object_vars($term));
    } else {
        return $term;
    }
}