get_the_terms

Retrieve the terms of the taxonomy that are attached to the post.

Description

(array|false|WP_Error) get_the_terms( (int|object) $post, (string) $taxonomy ); 

Returns (array|false|WP_Error)

Array of WP_Term objects on success, false if there are no terms or the post does not exist, WP_Error on failure.

Parameters (2)

0. $post (int|object)
Post ID or object.
1. $taxonomy (string)
Taxonomy name.

Usage

  1. if ( !function_exists( 'get_the_terms' ) ) { 
  2. require_once ABSPATH . WPINC . '/category-template.php'; 
  3.  
  4. // Post ID or object. 
  5. $post = null; 
  6.  
  7. // Taxonomy name. 
  8. $taxonomy = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_the_terms($post, $taxonomy); 
  12.  

Defined (1)

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

/wp-includes/category-template.php  
  1. function get_the_terms( $post, $taxonomy ) { 
  2. if ( ! $post = get_post( $post ) ) 
  3. return false; 
  4.  
  5. $terms = get_object_term_cache( $post->ID, $taxonomy ); 
  6. if ( false === $terms ) { 
  7. $terms = wp_get_object_terms( $post->ID, $taxonomy ); 
  8. if ( ! is_wp_error( $terms ) ) { 
  9. $term_ids = wp_list_pluck( $terms, 'term_id' ); 
  10. wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' ); 
  11.  
  12. /** 
  13. * Filters the list of terms attached to the given post. 
  14. * @since 3.1.0 
  15. * @param array|WP_Error $terms List of attached terms, or WP_Error on failure. 
  16. * @param int $post_id Post ID. 
  17. * @param string $taxonomy Name of the taxonomy. 
  18. */ 
  19. $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy ); 
  20.  
  21. if ( empty( $terms ) ) 
  22. return false; 
  23.  
  24. return $terms;