get_term_field

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

WordPress Version: 6.1

/**
 * Gets sanitized term field.
 *
 * The function is for contextual reasons and for simplicity of usage.
 *
 * @since 2.3.0
 * @since 4.4.0 The `$taxonomy` parameter was made optional. `$term` can also now accept a WP_Term object.
 *
 * @see sanitize_term_field()
 *
 * @param string      $field    Term field to fetch.
 * @param int|WP_Term $term     Term ID or object.
 * @param string      $taxonomy Optional. Taxonomy name. Default empty.
 * @param string      $context  Optional. How to sanitize term fields. Look at sanitize_term_field() for available options.
 *                              Default 'display'.
 * @return string|int|null|WP_Error Will return an empty string if $term is not an object or if $field is not set in $term.
 */
function get_term_field($field, $term, $taxonomy = '', $context = 'display')
{
    $term = get_term($term, $taxonomy);
    if (is_wp_error($term)) {
        return $term;
    }
    if (!is_object($term)) {
        return '';
    }
    if (!isset($term->{$field})) {
        return '';
    }
    return sanitize_term_field($field, $term->{$field}, $term->term_id, $term->taxonomy, $context);
}

WordPress Version: 5.9

/**
 * Get sanitized Term field.
 *
 * The function is for contextual reasons and for simplicity of usage.
 *
 * @since 2.3.0
 * @since 4.4.0 The `$taxonomy` parameter was made optional. `$term` can also now accept a WP_Term object.
 *
 * @see sanitize_term_field()
 *
 * @param string      $field    Term field to fetch.
 * @param int|WP_Term $term     Term ID or object.
 * @param string      $taxonomy Optional. Taxonomy name. Default empty.
 * @param string      $context  Optional. How to sanitize term fields. Look at sanitize_term_field() for available options.
 *                              Default 'display'.
 * @return string|int|null|WP_Error Will return an empty string if $term is not an object or if $field is not set in $term.
 */
function get_term_field($field, $term, $taxonomy = '', $context = 'display')
{
    $term = get_term($term, $taxonomy);
    if (is_wp_error($term)) {
        return $term;
    }
    if (!is_object($term)) {
        return '';
    }
    if (!isset($term->{$field})) {
        return '';
    }
    return sanitize_term_field($field, $term->{$field}, $term->term_id, $term->taxonomy, $context);
}

WordPress Version: 5.5

/**
 * Get sanitized Term field.
 *
 * The function is for contextual reasons and for simplicity of usage.
 *
 * @since 2.3.0
 * @since 4.4.0 The `$taxonomy` parameter was made optional. `$term` can also now accept a WP_Term object.
 *
 * @see sanitize_term_field()
 *
 * @param string      $field    Term field to fetch.
 * @param int|WP_Term $term     Term ID or object.
 * @param string      $taxonomy Optional. Taxonomy Name. Default empty.
 * @param string      $context  Optional. How to sanitize term fields. Look at sanitize_term_field() for available options.
 *                              Default 'display'.
 * @return string|int|null|WP_Error Will return an empty string if $term is not an object or if $field is not set in $term.
 */
function get_term_field($field, $term, $taxonomy = '', $context = 'display')
{
    $term = get_term($term, $taxonomy);
    if (is_wp_error($term)) {
        return $term;
    }
    if (!is_object($term)) {
        return '';
    }
    if (!isset($term->{$field})) {
        return '';
    }
    return sanitize_term_field($field, $term->{$field}, $term->term_id, $term->taxonomy, $context);
}

WordPress Version: 4.4

/**
 * Get sanitized Term field.
 *
 * The function is for contextual reasons and for simplicity of usage.
 *
 * @since 2.3.0
 * @since 4.4.0 The `$taxonomy` parameter was made optional. `$term` can also now accept a WP_Term object.
 *
 * @see sanitize_term_field()
 *
 * @param string      $field    Term field to fetch.
 * @param int|WP_Term $term     Term ID or object.
 * @param string      $taxonomy Optional. Taxonomy Name. Default empty.
 * @param string      $context  Optional, default is display. Look at sanitize_term_field() for available options.
 * @return string|int|null|WP_Error Will return an empty string if $term is not an object or if $field is not set in $term.
 */
function get_term_field($field, $term, $taxonomy = '', $context = 'display')
{
    $term = get_term($term, $taxonomy);
    if (is_wp_error($term)) {
        return $term;
    }
    if (!is_object($term)) {
        return '';
    }
    if (!isset($term->{$field})) {
        return '';
    }
    return sanitize_term_field($field, $term->{$field}, $term->term_id, $term->taxonomy, $context);
}

WordPress Version: 4.3

/**
 * Get sanitized Term field.
 *
 * Does checks for $term, based on the $taxonomy. The function is for contextual
 * reasons and for simplicity of usage. See sanitize_term_field() for more
 * information.
 *
 * @since 2.3.0
 *
 * @param string $field    Term field to fetch.
 * @param int    $term     Term ID.
 * @param string $taxonomy Taxonomy Name.
 * @param string $context  Optional, default is display. Look at sanitize_term_field() for available options.
 * @return string|int|null|WP_Error Will return an empty string if $term is not an object or if $field is not set in $term.
 */
function get_term_field($field, $term, $taxonomy, $context = 'display')
{
    $term = (int) $term;
    $term = get_term($term, $taxonomy);
    if (is_wp_error($term)) {
        return $term;
    }
    if (!is_object($term)) {
        return '';
    }
    if (!isset($term->{$field})) {
        return '';
    }
    return sanitize_term_field($field, $term->{$field}, $term->term_id, $taxonomy, $context);
}

WordPress Version: 4.1

/**
 * Get sanitized Term field.
 *
 * Does checks for $term, based on the $taxonomy. The function is for contextual
 * reasons and for simplicity of usage. See sanitize_term_field() for more
 * information.
 *
 * @since 2.3.0
 *
 * @param string $field Term field to fetch
 * @param int $term Term ID
 * @param string $taxonomy Taxonomy Name
 * @param string $context Optional, default is display. Look at sanitize_term_field() for available options.
 * @return mixed Will return an empty string if $term is not an object or if $field is not set in $term.
 */
function get_term_field($field, $term, $taxonomy, $context = 'display')
{
    $term = (int) $term;
    $term = get_term($term, $taxonomy);
    if (is_wp_error($term)) {
        return $term;
    }
    if (!is_object($term)) {
        return '';
    }
    if (!isset($term->{$field})) {
        return '';
    }
    return sanitize_term_field($field, $term->{$field}, $term->term_id, $taxonomy, $context);
}

WordPress Version: 3.9

/**
 * Get sanitized Term field.
 *
 * Does checks for $term, based on the $taxonomy. The function is for contextual
 * reasons and for simplicity of usage. See sanitize_term_field() for more
 * information.
 *
 * @since 2.3.0
 *
 * @uses sanitize_term_field() Passes the return value in sanitize_term_field on success.
 *
 * @param string $field Term field to fetch
 * @param int $term Term ID
 * @param string $taxonomy Taxonomy Name
 * @param string $context Optional, default is display. Look at sanitize_term_field() for available options.
 * @return mixed Will return an empty string if $term is not an object or if $field is not set in $term.
 */
function get_term_field($field, $term, $taxonomy, $context = 'display')
{
    $term = (int) $term;
    $term = get_term($term, $taxonomy);
    if (is_wp_error($term)) {
        return $term;
    }
    if (!is_object($term)) {
        return '';
    }
    if (!isset($term->{$field})) {
        return '';
    }
    return sanitize_term_field($field, $term->{$field}, $term->term_id, $taxonomy, $context);
}

WordPress Version: 3.7

/**
 * Get sanitized Term field.
 *
 * Does checks for $term, based on the $taxonomy. The function is for contextual
 * reasons and for simplicity of usage. See sanitize_term_field() for more
 * information.
 *
 * @package WordPress
 * @subpackage Taxonomy
 * @since 2.3.0
 *
 * @uses sanitize_term_field() Passes the return value in sanitize_term_field on success.
 *
 * @param string $field Term field to fetch
 * @param int $term Term ID
 * @param string $taxonomy Taxonomy Name
 * @param string $context Optional, default is display. Look at sanitize_term_field() for available options.
 * @return mixed Will return an empty string if $term is not an object or if $field is not set in $term.
 */
function get_term_field($field, $term, $taxonomy, $context = 'display')
{
    $term = (int) $term;
    $term = get_term($term, $taxonomy);
    if (is_wp_error($term)) {
        return $term;
    }
    if (!is_object($term)) {
        return '';
    }
    if (!isset($term->{$field})) {
        return '';
    }
    return sanitize_term_field($field, $term->{$field}, $term->term_id, $taxonomy, $context);
}