get_term() WP 1.0
Get all Term data from database by Term ID.
The usage of the get_term function is to apply filters to a term object. It is possible to get a term object from the database before applying the filters.
$term ID must be part of $taxonomy, to get from the database. Failure, might be able to be captured by the hooks. Failure would be the same value as $wpdb returns for the get_row method.
There are two hooks, one is specifically for each term, named 'get_term', and the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the term object, and the taxonomy name as parameters. Both hooks are expected to return a Term object.
'get_term' hook - Takes two parameters the term Object and the taxonomy name. Must return term object. Used in get_term() as a catch-all filter for every $term.
'get_(taxonomy)' hook - Takes two parameters the term Object and the taxonomy name. Must return term object. $taxonomy will be the taxonomy name, so for example, if 'category', it would be 'get_category' as the filter name. Useful for custom taxonomies or plugging into default taxonomies.
Hooks from the function
Return
WP_Term/Array/WP_Error/null. WP_Term instance (or array) on success, depending on the $output value. WP_Error if $taxonomy does not exist. Null for miscellaneous failure.
Usage
get_term( $term, $taxonomy, $output, $filter );
- $term(int/WP_Term/object) (required)
- If integer, term data will be fetched from the database, or from the cache if available. If stdClass object (as in the results of a database query), will apply filters and return a WP_Term object with the $term data. If WP_Term, will return $term.
- $taxonomy(string)
- Taxonomy name that $term is part of.
Default: '' - $output(string)
- The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Term object, an associative array, or a numeric array, respectively.
Default: OBJECT - $filter(string)
- How to sanitize term fields.
Default: 'raw'
Notes
- See: sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
Changelog
Since 2.3.0 | Introduced. |
Since 4.4.0 | Converted to return a WP_Term object if $output is OBJECT. The $taxonomy parameter was made optional. |
Code of get_term() get term WP 5.6
Related Functions
From tag: Term (taxonomies terms)
- get_term_by()
- get_term_children()
- get_term_field()
- get_term_link()
- get_terms()
- get_the_term_list()
- get_the_terms()
More from category: Any taxonomy
- edit_term_link()
- get_edit_term_link()
- get_taxonomies()
- get_taxonomy()
- is_taxonomy_hierarchical()
- register_taxonomy()