get_term_field

Get sanitized Term field.

Description

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

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

Returns (string|int|null|WP_Error)

Will return an empty string if $term is not an object or if $field is not set in $term.

Parameters (4)

0. $field (string)
Term field to fetch.
1. $term (int|WP_Term)
Term ID or object.
2. $taxonomy — Optional. (string) => ''
Taxonomy Name. Default empty.
3. $context — Optional. (string) => 'display'
Optional, default is display. Look at sanitize_term_field() for available options.

Usage

  1. if ( !function_exists( 'get_term_field' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // Term field to fetch. 
  5. $field = ''; 
  6.  
  7. // Term ID or object. 
  8. $term = null; 
  9.  
  10. // Optional. Taxonomy Name. Default empty. 
  11. $taxonomy = ''; 
  12.  
  13. // Optional, default is display. Look at sanitize_term_field() for available options. 
  14. $context = 'display'; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = get_term_field($field, $term, $taxonomy, $context); 
  18.  

Defined (1)

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

/wp-includes/taxonomy.php  
  1. function get_term_field( $field, $term, $taxonomy = '', $context = 'display' ) { 
  2. $term = get_term( $term, $taxonomy ); 
  3. if ( is_wp_error($term) ) 
  4. return $term; 
  5.  
  6. if ( !is_object($term) ) 
  7. return ''; 
  8.  
  9. if ( !isset($term->$field) ) 
  10. return ''; 
  11.  
  12. return sanitize_term_field( $field, $term->$field, $term->term_id, $term->taxonomy, $context );