Using WordPress ‘delete_term_meta()’ PHP function

The delete_term_meta() WordPress PHP function is used to remove metadata matching criteria from a term.

Usage

Here’s a generic example of how to use the delete_term_meta() function:

delete_term_meta( $term_id, $meta_key, $meta_value );

In this case, the function will remove the metadata with key $meta_key and value $meta_value from the term with id $term_id.

Parameters

  • $term_id (int): The ID of the term from which metadata is to be removed.
  • $meta_key (string): The name of the metadata to be removed.
  • $meta_value (mixed): Optional. If provided, only rows that match this value will be removed. This value must be serializable if non-scalar. Default is an empty string.

More information

See WordPress Developer Resources: delete_term_meta()

Examples

Delete Term Metadata By Key

This code will delete the metadata with the key ‘color’ for the term with ID 123.

$term_id = 123;
$meta_key = 'color';

delete_term_meta( $term_id, $meta_key );

Delete Term Metadata By Key and Value

This code will delete the metadata with the key ‘color’ and value ‘blue’ for the term with ID 123.

$term_id = 123;
$meta_key = 'color';
$meta_value = 'blue';

delete_term_meta( $term_id, $meta_key, $meta_value );

Delete Multiple Term Metadata By Key

This code will delete all the metadata with the key ‘color’ for the terms with IDs 123, 124, and 125.

$term_ids = array(123, 124, 125);
$meta_key = 'color';

foreach( $term_ids as $term_id ) {
    delete_term_meta( $term_id, $meta_key );
}

Delete Multiple Term Metadata By Key and Value

This code will delete the metadata with the key ‘color’ and value ‘blue’ for the terms with IDs 123, 124, and 125.

$term_ids = array(123, 124, 125);
$meta_key = 'color';
$meta_value = 'blue';

foreach( $term_ids as $term_id ) {
    delete_term_meta( $term_id, $meta_key, $meta_value );
}

Delete All Metadata for a Term

This code will delete all metadata for the term with ID 123.

$term_id = 123;
$meta_keys = get_term_meta( $term_id );

foreach( $meta_keys as $meta_key ) {
    delete_term_meta( $term_id, $meta_key );
}