get_attachment_taxonomies

Definition:
function get_attachment_taxonomies($attachment) {}

Retrieve taxonomies attached to the attachment.

Parameters

  • int|array|object $attachment: Attachment ID, Attachment data array, or Attachment data object.

Return values

returns:Empty array on failure. List of taxonomies on success.

Source code

function get_attachment_taxonomies($attachment) {

	if ( is_int( $attachment ) )

		$attachment = get_post($attachment);

	else if ( is_array($attachment) )

		$attachment = (object) $attachment;



	if ( ! is_object($attachment) )

		return array();



	$filename = basename($attachment->guid);



	$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) )

			$taxonomies = array_merge($taxonomies, $taxes);



	return array_unique($taxonomies);

}

1162

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: