get_term_meta()WP 4.4.0

Retrieves metadata for a term.

1 time — 0.000125 sec (fast) | 50000 times — 0.20 sec (very fast) | PHP 7.1.2, WP 4.7.3

No Hooks.

Return

Mixed. An array of values if $single is false. The value of the meta field if $single is true. False for an invalid $term_id (non-numeric, zero, or negative value). An empty string if a valid but non-existing term ID is passed.

Usage

get_term_meta( $term_id, $key, $single );
$term_id(int) (required)
Term ID.
$key(string)
The meta key to retrieve. By default, returns data for all keys.
Default: ''
$single(true|false)
Whether to return a single value. This parameter has no effect if $key is not specified.
Default: false

Examples

1

#1 Get all values of test_meta_field term meta field

$fields =  get_term_meta( 10 , 'test_meta_field', false );

if( $fields ){

	print_r( $fields );

	/* will output
	Array
	(
		[0] => Hello world
		[1] => Hello world
	)
	*/
}
else {
	// no metadata...
}
0

#2 Get the value of the meta on the term archive page

Suppose we have a meta field test_meta_field category 65 that contains (string) What a nice day and we need to display this string on the category page.

// the current category, let's assume we visit category 65 page
$term_id = get_query_var( 'cat' ); 

echo get_term_meta( $term_id, 'test_meta_field', true ); //> What a nice day

Another way. We can use get_queried_object_id() function to get current category id.

// Usage in archive or category (taxonomy) template
$term_id = get_queried_object_id();

echo get_term_meta( $term_id, 'test_meta_field', true ); 

Changelog

Since 4.4.0 Introduced.

get_term_meta() code WP 6.4.3

function get_term_meta( $term_id, $key = '', $single = false ) {
	return get_metadata( 'term', $term_id, $key, $single );
}