Using WordPress ‘get_term_meta()’ PHP function

The get_term_meta() WordPress PHP function retrieves metadata for a term.

Usage

get_term_meta( $term_id, $key = '', $single = false )

Custom Example:

// Get term metadata for term ID 1
$term_meta = get_term_meta(1);

Parameters

  • $term_id (int): Required. The term ID.
  • $key (string): Optional. The meta key to retrieve. By default, returns data for all keys. Default: ”.
  • $single (bool): Optional. Whether to return a single value. This parameter has no effect if $key is not specified. Default: false.

More information

See WordPress Developer Resources: get_term_meta()

Examples

Get a specific term meta value

This example shows how to get the value of a specific term meta key for a term with the ID of 10.

// Get the term description for term ID 10
$term_description = get_term_meta( 10, 'description', true );

Get all term meta values

This example retrieves all term meta values for a term with the ID of 5.

$term_meta_values = get_term_meta( 5 );

foreach ( $term_meta_values as $key => $value ) {
    echo "Key: " . $key . " | Value: " . $value[0] . "<br>";
}

Get a term meta value in a taxonomy template

This example retrieves a term meta value within a taxonomy template.

$term_image = get_term_meta( get_queried_object_id(), '_myprefix_term_image', true );

Update term meta value

This example updates a term meta value and then retrieves the updated value for a term with the ID of 7.

update_term_meta( 7, 'color', 'blue' );
$updated_term_color = get_term_meta( 7, 'color', true );

Delete term meta value

This example deletes a term meta value and then checks if the value has been deleted for a term with the ID of 3.

delete_term_meta( 3, 'color' );
$deleted_term_color = get_term_meta( 3, 'color', true );

if ( empty( $deleted_term_color ) ) {
    echo "Term meta value has been deleted.";
}