CMB2_Type_Taxonomy_Base

CMB Taxonomy base field type.

Defined (1)

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

/includes/types/CMB2_Type_Taxonomy_Base.php  
  1. abstract class CMB2_Type_Taxonomy_Base extends CMB2_Type_Multi_Base { 
  2.  
  3. /** 
  4. * Checks if we can get a post object, and if so, uses `get_the_terms` which utilizes caching 
  5. * @since 1.0.2 
  6. * @return mixed Array of terms on success 
  7. */ 
  8. public function get_object_terms() { 
  9. $object_id = $this->field->object_id; 
  10. $taxonomy = $this->field->args( 'taxonomy' ); 
  11.  
  12. if ( ! $post = get_post( $object_id ) ) { 
  13.  
  14. $cache_key = "cmb-cache-{$taxonomy}-{$object_id}"; 
  15.  
  16. // Check cache 
  17. $cached = get_transient( $cache_key ); 
  18. if ( $cached ) { 
  19. return $cached; 
  20.  
  21. $cached = wp_get_object_terms( $object_id, $taxonomy ); 
  22. // Do our own (minimal) caching. Long enough for a page-load. 
  23. set_transient( $cache_key, $cached, 60 ); 
  24. return $cached; 
  25.  
  26. // WP caches internally so it's better to use 
  27. return get_the_terms( $post, $taxonomy ); 
  28.  
  29. /** 
  30. * Wrapper for `get_terms` to account for changes in WP 4.6 where taxonomy is expected 
  31. * as part of the arguments. 
  32. * @since 2.2.2 
  33. * @return mixed Array of terms on success 
  34. */ 
  35. public function get_terms() { 
  36. return CMB2_Utils::wp_at_least( '4.5.0' ) 
  37. ? get_terms( array( 'taxonomy' => $this->field->args( 'taxonomy' ), 'hide_empty' => false ) ) 
  38. : get_terms( $this->field->args( 'taxonomy' ), 'hide_empty=0' ); 
  39.