WP_Term

Core class used to implement the WP_Term object.

Defined (1)

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

/wp-includes/class-wp-term.php  
  1. final class WP_Term { 
  2.  
  3. /** 
  4. * Term ID. 
  5. * @since 4.4.0 
  6. * @access public 
  7. * @var int 
  8. */ 
  9. public $term_id; 
  10.  
  11. /** 
  12. * The term's name. 
  13. * @since 4.4.0 
  14. * @access public 
  15. * @var string 
  16. */ 
  17. public $name = ''; 
  18.  
  19. /** 
  20. * The term's slug. 
  21. * @since 4.4.0 
  22. * @access public 
  23. * @var string 
  24. */ 
  25. public $slug = ''; 
  26.  
  27. /** 
  28. * The term's term_group. 
  29. * @since 4.4.0 
  30. * @access public 
  31. * @var string 
  32. */ 
  33. public $term_group = ''; 
  34.  
  35. /** 
  36. * Term Taxonomy ID. 
  37. * @since 4.4.0 
  38. * @access public 
  39. * @var int 
  40. */ 
  41. public $term_taxonomy_id = 0; 
  42.  
  43. /** 
  44. * The term's taxonomy name. 
  45. * @since 4.4.0 
  46. * @access public 
  47. * @var string 
  48. */ 
  49. public $taxonomy = ''; 
  50.  
  51. /** 
  52. * The term's description. 
  53. * @since 4.4.0 
  54. * @access public 
  55. * @var string 
  56. */ 
  57. public $description = ''; 
  58.  
  59. /** 
  60. * ID of a term's parent term. 
  61. * @since 4.4.0 
  62. * @access public 
  63. * @var int 
  64. */ 
  65. public $parent = 0; 
  66.  
  67. /** 
  68. * Cached object count for this term. 
  69. * @since 4.4.0 
  70. * @access public 
  71. * @var int 
  72. */ 
  73. public $count = 0; 
  74.  
  75. /** 
  76. * Stores the term object's sanitization level. 
  77. * Does not correspond to a database field. 
  78. * @since 4.4.0 
  79. * @access public 
  80. * @var string 
  81. */ 
  82. public $filter = 'raw'; 
  83.  
  84. /** 
  85. * Retrieve WP_Term instance. 
  86. * @since 4.4.0 
  87. * @access public 
  88. * @static 
  89. * @global wpdb $wpdb WordPress database abstraction object. 
  90. * @param int $term_id Term ID. 
  91. * @param string $taxonomy Optional. Limit matched terms to those matching `$taxonomy`. Only used for 
  92. * disambiguating potentially shared terms. 
  93. * @return WP_Term|WP_Error|false Term object, if found. WP_Error if `$term_id` is shared between taxonomies and 
  94. * there's insufficient data to distinguish which term is intended. 
  95. * False for other failures. 
  96. */ 
  97. public static function get_instance( $term_id, $taxonomy = null ) { 
  98. global $wpdb; 
  99.  
  100. $term_id = (int) $term_id; 
  101. if ( ! $term_id ) { 
  102. return false; 
  103.  
  104. $_term = wp_cache_get( $term_id, 'terms' ); 
  105.  
  106. // If there isn't a cached version, hit the database. 
  107. if ( ! $_term || ( $taxonomy && $taxonomy !== $_term->taxonomy ) ) { 
  108. // Grab all matching terms, in case any are shared between taxonomies. 
  109. $terms = $wpdb->get_results( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id = %d", $term_id ) ); 
  110. if ( ! $terms ) { 
  111. return false; 
  112.  
  113. // If a taxonomy was specified, find a match. 
  114. if ( $taxonomy ) { 
  115. foreach ( $terms as $match ) { 
  116. if ( $taxonomy === $match->taxonomy ) { 
  117. $_term = $match; 
  118. break; 
  119.  
  120. // If only one match was found, it's the one we want. 
  121. } elseif ( 1 === count( $terms ) ) { 
  122. $_term = reset( $terms ); 
  123.  
  124. // Otherwise, the term must be shared between taxonomies. 
  125. } else { 
  126. // If the term is shared only with invalid taxonomies, return the one valid term. 
  127. foreach ( $terms as $t ) { 
  128. if ( ! taxonomy_exists( $t->taxonomy ) ) { 
  129. continue; 
  130.  
  131. // Only hit if we've already identified a term in a valid taxonomy. 
  132. if ( $_term ) { 
  133. return new WP_Error( 'ambiguous_term_id', __( 'Term ID is shared between multiple taxonomies' ), $term_id ); 
  134.  
  135. $_term = $t; 
  136.  
  137. if ( ! $_term ) { 
  138. return false; 
  139.  
  140. // Don't return terms from invalid taxonomies. 
  141. if ( ! taxonomy_exists( $_term->taxonomy ) ) { 
  142. return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); 
  143.  
  144. $_term = sanitize_term( $_term, $_term->taxonomy, 'raw' ); 
  145.  
  146. // Don't cache terms that are shared between taxonomies. 
  147. if ( 1 === count( $terms ) ) { 
  148. wp_cache_add( $term_id, $_term, 'terms' ); 
  149.  
  150. $term_obj = new WP_Term( $_term ); 
  151. $term_obj->filter( $term_obj->filter ); 
  152.  
  153. return $term_obj; 
  154.  
  155. /** 
  156. * Constructor. 
  157. * @since 4.4.0 
  158. * @access public 
  159. * @param WP_Term|object $term Term object. 
  160. */ 
  161. public function __construct( $term ) { 
  162. foreach ( get_object_vars( $term ) as $key => $value ) { 
  163. $this->$key = $value; 
  164.  
  165. /** 
  166. * Sanitizes term fields, according to the filter type provided. 
  167. * @since 4.4.0 
  168. * @access public 
  169. * @param string $filter Filter context. Accepts 'edit', 'db', 'display', 'attribute', 'js', 'raw'. 
  170. */ 
  171. public function filter( $filter ) { 
  172. sanitize_term( $this, $this->taxonomy, $filter ); 
  173.  
  174. /** 
  175. * Converts an object to array. 
  176. * @since 4.4.0 
  177. * @access public 
  178. * @return array Object as array. 
  179. */ 
  180. public function to_array() { 
  181. return get_object_vars( $this ); 
  182.  
  183. /** 
  184. * Getter. 
  185. * @since 4.4.0 
  186. * @access public 
  187. * @param string $key Property to get. 
  188. * @return mixed Property value. 
  189. */ 
  190. public function __get( $key ) { 
  191. switch ( $key ) { 
  192. case 'data' : 
  193. $data = new stdClass(); 
  194. $columns = array( 'term_id', 'name', 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', 'parent', 'count' ); 
  195. foreach ( $columns as $column ) { 
  196. $data->{$column} = isset( $this->{$column} ) ? $this->{$column} : null; 
  197.  
  198. return sanitize_term( $data, $data->taxonomy, 'raw' );