wp_get_post_terms

Retrieve the terms for a post.

Description

(array|WP_Error) wp_get_post_terms( (int) $post_id = 0, (string) $taxonomy = 'post_tag', (array) $args = array() ); 

There is only one default for this function, called fields and by default is set to all.. There are other defaults that can be overridden in wp_get_object_terms().

Returns (array|WP_Error)

List of post terms or empty array if no terms were found. WP_Error object if `$taxonomy` doesn't exist.

Parameters (3)

0. $post_id — Optional. (int)
The Post ID. Does not default to the ID of the global $post. Default 0.
1. $taxonomy — Optional. (string) => 'post_tag'
The taxonomy for which to retrieve terms. Default post_tag..
2. $args — Optional. (array) => array()
wp_get_object_terms() arguments. Default empty array.

Usage

  1. if ( !function_exists( 'wp_get_post_terms' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // Optional. The Post ID. Does not default to the ID of the 
  5. // global $post. Default 0. 
  6. $post_id = -1; 
  7.  
  8. // Optional. The taxonomy for which to retrieve terms. Default 'post_tag'. 
  9. $taxonomy = 'post_tag'; 
  10.  
  11. // Optional. wp_get_object_terms() arguments. Default empty array. 
  12. $args = array(); 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = wp_get_post_terms($post_id, $taxonomy, $args); 
  16.  

Defined (1)

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

/wp-includes/post.php  
  1. function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) { 
  2. $post_id = (int) $post_id; 
  3.  
  4. $defaults = array('fields' => 'all'); 
  5. $args = wp_parse_args( $args, $defaults ); 
  6.  
  7. $tags = wp_get_object_terms($post_id, $taxonomy, $args); 
  8.  
  9. return $tags;