3 * Taxonomy API: WP_Term class
11 * Core class used to implement the WP_Term object.
45 * The term's term_group.
51 public $term_group = '';
60 public $term_taxonomy_id = 0;
63 * The term's taxonomy name.
69 public $taxonomy = '';
72 * The term's description.
78 public $description = '';
81 * ID of a term's parent term.
90 * Cached object count for this term.
99 * Stores the term object's sanitization level.
101 * Does not correspond to a database field.
107 public $filter = 'raw';
110 * Retrieve WP_Term instance.
116 * @global wpdb $wpdb WordPress database abstraction object.
118 * @param int $term_id Term ID.
119 * @param string $taxonomy Optional. Limit matched terms to those matching `$taxonomy`. Only used for
120 * disambiguating potentially shared terms.
121 * @return WP_Term|WP_Error|false Term object, if found. WP_Error if `$term_id` is shared between taxonomies and
122 * there's insufficient data to distinguish which term is intended.
123 * False for other failures.
125 public static function get_instance( $term_id, $taxonomy = null ) {
128 $term_id = (int) $term_id;
133 $_term = wp_cache_get( $term_id, 'terms' );
135 // If there isn't a cached version, hit the database.
136 if ( ! $_term || ( $taxonomy && $taxonomy !== $_term->taxonomy ) ) {
137 // Grab all matching terms, in case any are shared between taxonomies.
138 $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 ) );
143 // If a taxonomy was specified, find a match.
145 foreach ( $terms as $match ) {
146 if ( $taxonomy === $match->taxonomy ) {
152 // If only one match was found, it's the one we want.
153 } elseif ( 1 === count( $terms ) ) {
154 $_term = reset( $terms );
156 // Otherwise, the term must be shared between taxonomies.
158 // If the term is shared only with invalid taxonomies, return the one valid term.
159 foreach ( $terms as $t ) {
160 if ( ! taxonomy_exists( $t->taxonomy ) ) {
164 // Only hit if we've already identified a term in a valid taxonomy.
166 return new WP_Error( 'ambiguous_term_id', __( 'Term ID is shared between multiple taxonomies' ), $term_id );
177 // Don't return terms from invalid taxonomies.
178 if ( ! taxonomy_exists( $_term->taxonomy ) ) {
179 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
182 $_term = sanitize_term( $_term, $_term->taxonomy, 'raw' );
184 // Don't cache terms that are shared between taxonomies.
185 if ( 1 === count( $terms ) ) {
186 wp_cache_add( $term_id, $_term, 'terms' );
190 $term_obj = new WP_Term( $_term );
191 $term_obj->filter( $term_obj->filter );
202 * @param WP_Term|object $term Term object.
204 public function __construct( $term ) {
205 foreach ( get_object_vars( $term ) as $key => $value ) {
206 $this->$key = $value;
211 * Sanitizes term fields, according to the filter type provided.
216 * @param string $filter Filter context. Accepts 'edit', 'db', 'display', 'attribute', 'js', 'raw'.
218 public function filter( $filter ) {
219 sanitize_term( $this, $this->taxonomy, $filter );
223 * Converts an object to array.
228 * @return array Object as array.
230 public function to_array() {
231 return get_object_vars( $this );
240 * @param string $key Property to get.
241 * @return mixed Property value.
243 public function __get( $key ) {
246 $data = new stdClass();
247 $columns = array( 'term_id', 'name', 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', 'parent', 'count' );
248 foreach ( $columns as $column ) {
249 $data->{$column} = isset( $this->{$column} ) ? $this->{$column} : null;
252 return sanitize_term( $data, $data->taxonomy, 'raw' );