add_term_meta

Adds metadata to a term.

Description

(int|WP_Error|bool) add_term_meta( (int) $term_id, (string) $meta_key, (mixed) $meta_value, (bool) $unique = false ); 

Returns (int|WP_Error|bool)

Meta ID on success. WP_Error when term_id is ambiguous between taxonomies. False on failure.

Parameters (4)

0. $term_id (int)
The term id.
1. $meta_key (string)
Metadata name.
2. $meta_value (mixed)
The meta value.
3. $unique — Optional. (bool) => false
Whether to bail if an entry with the same key is found for the term. Default false.

Usage

  1. if ( !function_exists( 'add_term_meta' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // The term id. 
  5. $term_id = -1; 
  6.  
  7. // Metadata name. 
  8. $meta_key = ''; 
  9.  
  10. // The meta value. 
  11. $meta_value = null; 
  12.  
  13. // Optional. Whether to bail if an entry with the same key is found for the term. 
  14. // Default false. 
  15. $unique = false; 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = add_term_meta($term_id, $meta_key, $meta_value, $unique); 
  19.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/taxonomy.php  
  1. function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) { 
  2. // Bail if term meta table is not installed. 
  3. if ( get_option( 'db_version' ) < 34370 ) { 
  4. return false; 
  5.  
  6. if ( wp_term_is_shared( $term_id ) ) { 
  7. return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.'), $term_id ); 
  8.  
  9. $added = add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique ); 
  10.  
  11. // Bust term query cache. 
  12. if ( $added ) { 
  13. wp_cache_set( 'last_changed', microtime(), 'terms' ); 
  14.  
  15. return $added;