get_attachment_taxonomies

Retrieves taxonomies attached to given the attachment.

Description

(array) get_attachment_taxonomies( (int|array|object) $attachment, (string) $output = 'names' ); 

Returns (array)

Empty array on failure. List of taxonomies on success.

Parameters (2)

0. $attachment (int|array|object)
Attachment ID, data array, or data object.
1. $output — Optional. (string) => 'names'
Output type. names to return an array of taxonomy names, or objects. to return an array of taxonomy objects. Default is names .

Usage

  1. if ( !function_exists( 'get_attachment_taxonomies' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Attachment ID, data array, or data object. 
  5. $attachment = null; 
  6. $output = 'names'; 
  7.  
  8. // NOTICE! Understand what this does before running. 
  9. $result = get_attachment_taxonomies($attachment, $output); 
  10.  

Defined (1)

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

/wp-includes/media.php  
  1. function get_attachment_taxonomies( $attachment, $output = 'names' ) { 
  2. if ( is_int( $attachment ) ) { 
  3. $attachment = get_post( $attachment ); 
  4. } elseif ( is_array( $attachment ) ) { 
  5. $attachment = (object) $attachment; 
  6. if ( ! is_object($attachment) ) 
  7. return array(); 
  8.  
  9. $file = get_attached_file( $attachment->ID ); 
  10. $filename = basename( $file ); 
  11.  
  12. $objects = array('attachment'); 
  13.  
  14. if ( false !== strpos($filename, '.') ) 
  15. $objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1); 
  16. if ( !empty($attachment->post_mime_type) ) { 
  17. $objects[] = 'attachment:' . $attachment->post_mime_type; 
  18. if ( false !== strpos($attachment->post_mime_type, '/') ) 
  19. foreach ( explode('/', $attachment->post_mime_type) as $token ) 
  20. if ( !empty($token) ) 
  21. $objects[] = "attachment:$token"; 
  22.  
  23. $taxonomies = array(); 
  24. foreach ( $objects as $object ) { 
  25. if ( $taxes = get_object_taxonomies( $object, $output ) ) { 
  26. $taxonomies = array_merge( $taxonomies, $taxes ); 
  27.  
  28. if ( 'names' === $output ) { 
  29. $taxonomies = array_unique( $taxonomies ); 
  30.  
  31. return $taxonomies;