get_term

Get all Term data from database by Term ID.

Description

(array|WP_Term|WP_Error|null) get_term( (int|WP_Term|object) $term, (string) $taxonomy = '', (constant) $output = OBJECT, (string) $filter = 'raw' ); 

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.

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.

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.

Returns (array|WP_Term|WP_Error|null)

Object of the type specified by `$output` on success. When `$output` is 'OBJECT', a WP_Term instance is returned. If taxonomy does not exist, a WP_Error is returned. Returns null for miscellaneous failure.

Parameters (4)

0. $term (int|WP_Term|object)
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 corresponding to the $term, data. If WP_Term , will return $term,.
1. $taxonomy — Optional. (string) => ''
Taxonomy name that $term is part of.
2. $output — Optional. (constant) => OBJECT
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.
3. $filter — Optional. (string) => 'raw'
Optional, default is raw or no WordPress defined filter will applied.

Usage

  1. if ( !function_exists( 'get_term' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3. $term = null; 
  4.  
  5. // Optional. Taxonomy name that $term is part of. 
  6. $taxonomy = ''; 
  7.  
  8. // Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to 
  9. // a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT. 
  10. $output = OBJECT; 
  11.  
  12. // Optional, default is raw or no WordPress defined filter will applied. 
  13. $filter = 'raw'; 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = get_term($term, $taxonomy, $output, $filter); 
  17.  

Defined (1)

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

/wp-includes/taxonomy.php  
  1. function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { 
  2. if ( empty( $term ) ) { 
  3. return new WP_Error( 'invalid_term', __( 'Empty Term' ) ); 
  4.  
  5. if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) { 
  6. return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); 
  7.  
  8. if ( $term instanceof WP_Term ) { 
  9. $_term = $term; 
  10. } elseif ( is_object( $term ) ) { 
  11. if ( empty( $term->filter ) || 'raw' === $term->filter ) { 
  12. $_term = sanitize_term( $term, $taxonomy, 'raw' ); 
  13. $_term = new WP_Term( $_term ); 
  14. } else { 
  15. $_term = WP_Term::get_instance( $term->term_id ); 
  16. } else { 
  17. $_term = WP_Term::get_instance( $term, $taxonomy ); 
  18.  
  19. if ( is_wp_error( $_term ) ) { 
  20. return $_term; 
  21. } elseif ( ! $_term ) { 
  22. return null; 
  23.  
  24. /** 
  25. * Filters a term. 
  26. * @since 2.3.0 
  27. * @since 4.4.0 `$_term` can now also be a WP_Term object. 
  28. * @param int|WP_Term $_term Term object or ID. 
  29. * @param string $taxonomy The taxonomy slug. 
  30. */ 
  31. $_term = apply_filters( 'get_term', $_term, $taxonomy ); 
  32.  
  33. /** 
  34. * Filters a taxonomy. 
  35. * The dynamic portion of the filter name, `$taxonomy`, refers 
  36. * to the taxonomy slug. 
  37. * @since 2.3.0 
  38. * @since 4.4.0 `$_term` can now also be a WP_Term object. 
  39. * @param int|WP_Term $_term Term object or ID. 
  40. * @param string $taxonomy The taxonomy slug. 
  41. */ 
  42. $_term = apply_filters( "get_{$taxonomy}", $_term, $taxonomy ); 
  43.  
  44. // Bail if a filter callback has changed the type of the `$_term` object. 
  45. if ( ! ( $_term instanceof WP_Term ) ) { 
  46. return $_term; 
  47.  
  48. // Sanitize term, according to the specified filter. 
  49. $_term->filter( $filter ); 
  50.  
  51. if ( $output == ARRAY_A ) { 
  52. return $_term->to_array(); 
  53. } elseif ( $output == ARRAY_N ) { 
  54. return array_values( $_term->to_array() ); 
  55.  
  56. return $_term;