get_term_field() WordPress Function

The get_term_field() function is used to retrieve the value of a specific field for a given term. The function takes two arguments: the term ID and the field name. The field name can be any of the following: - name - slug - term_group - term_taxonomy_id - taxonomy - description - parent - count The function returns the value of the specified field for the given term. If the field does not exist, the function returns false.

get_term_field( string $field, int|WP_Term $term, string $taxonomy = '', string $context = 'display' ) #

Gets sanitized term field.


Description

The function is for contextual reasons and for simplicity of usage.

Top ↑

See also


Top ↑

Parameters

$field

(string)(Required)Term field to fetch.

$term

(int|WP_Term)(Required)Term ID or object.

$taxonomy

(string)(Optional) Taxonomy name.

Default value: ''

$context

(string)(Optional) How to sanitize term fields. Look at sanitize_term_field() for available options.

Default value: 'display'


Top ↑

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.


Top ↑

Source

File: wp-includes/taxonomy.php

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 );
}


Top ↑

Changelog

Changelog
VersionDescription
4.4.0The $taxonomy parameter was made optional. $term can also now accept a WP_Term object.
2.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More