get_attachment_taxonomies
Retrieves taxonomies attached to given the attachment.
Description
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
if ( !function_exists( 'get_attachment_taxonomies' ) ) { require_once ABSPATH . WPINC . '/media.php'; } // Attachment ID, data array, or data object. $attachment = null; $output = 'names'; // NOTICE! Understand what this does before running. $result = get_attachment_taxonomies($attachment, $output);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/media.php
- function get_attachment_taxonomies( $attachment, $output = 'names' ) {
- if ( is_int( $attachment ) ) {
- $attachment = get_post( $attachment );
- } elseif ( is_array( $attachment ) ) {
- $attachment = (object) $attachment;
- }
- if ( ! is_object($attachment) )
- return array();
- $file = get_attached_file( $attachment->ID );
- $filename = basename( $file );
- $objects = array('attachment');
- if ( false !== strpos($filename, '.') )
- $objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1);
- if ( !empty($attachment->post_mime_type) ) {
- $objects[] = 'attachment:' . $attachment->post_mime_type;
- if ( false !== strpos($attachment->post_mime_type, '/') )
- foreach ( explode('/', $attachment->post_mime_type) as $token )
- if ( !empty($token) )
- $objects[] = "attachment:$token";
- }
- $taxonomies = array();
- foreach ( $objects as $object ) {
- if ( $taxes = get_object_taxonomies( $object, $output ) ) {
- $taxonomies = array_merge( $taxonomies, $taxes );
- }
- }
- if ( 'names' === $output ) {
- $taxonomies = array_unique( $taxonomies );
- }
- return $taxonomies;
- }