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 if ( ! is_numeric( $term_id ) || $term_id != floor( $term_id ) || ! $term_id ) {
132 $term_id = (int) $term_id;
134 $_term = wp_cache_get( $term_id, 'terms' );
136 // If there isn't a cached version, hit the database.
137 if ( ! $_term || ( $taxonomy && $taxonomy !== $_term->taxonomy ) ) {
138 // Grab all matching terms, in case any are shared between taxonomies.
139 $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 ) );
144 // If a taxonomy was specified, find a match.
146 foreach ( $terms as $match ) {
147 if ( $taxonomy === $match->taxonomy ) {
153 // If only one match was found, it's the one we want.
154 } elseif ( 1 === count( $terms ) ) {
155 $_term = reset( $terms );
157 // Otherwise, the term must be shared between taxonomies.
159 // If the term is shared only with invalid taxonomies, return the one valid term.
160 foreach ( $terms as $t ) {
161 if ( ! taxonomy_exists( $t->taxonomy ) ) {
165 // Only hit if we've already identified a term in a valid taxonomy.
167 return new WP_Error( 'ambiguous_term_id', __( 'Term ID is shared between multiple taxonomies' ), $term_id );
178 // Don't return terms from invalid taxonomies.
179 if ( ! taxonomy_exists( $_term->taxonomy ) ) {
180 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
183 $_term = sanitize_term( $_term, $_term->taxonomy, 'raw' );
185 // Don't cache terms that are shared between taxonomies.
186 if ( 1 === count( $terms ) ) {
187 wp_cache_add( $term_id, $_term, 'terms' );
191 $term_obj = new WP_Term( $_term );
192 $term_obj->filter( $term_obj->filter );
203 * @param WP_Term|object $term Term object.
205 public function __construct( $term ) {
206 foreach ( get_object_vars( $term ) as $key => $value ) {
207 $this->$key = $value;
212 * Sanitizes term fields, according to the filter type provided.
217 * @param string $filter Filter context. Accepts 'edit', 'db', 'display', 'attribute', 'js', 'raw'.
219 public function filter( $filter ) {
220 sanitize_term( $this, $this->taxonomy, $filter );
224 * Converts an object to array.
229 * @return array Object as array.
231 public function to_array() {
232 return get_object_vars( $this );
241 * @param string $key Property to get.
242 * @return mixed Property value.
244 public function __get( $key ) {
247 $data = new stdClass();
248 $columns = array( 'term_id', 'name', 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', 'parent', 'count' );
249 foreach ( $columns as $column ) {
250 $data->{$column} = isset( $this->{$column} ) ? $this->{$column} : null;
253 return sanitize_term( $data, $data->taxonomy, 'raw' );