get_attachment_taxonomies

The timeline below displays how wordpress function get_attachment_taxonomies has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Retrieves taxonomies attached to given the attachment.
 *
 * @since 2.5.0
 * @since 4.7.0 Introduced the `$output` parameter.
 *
 * @param int|array|object $attachment Attachment ID, data array, or data object.
 * @param string           $output     Output type. 'names' to return an array of taxonomy names,
 *                                     or 'objects' to return an array of taxonomy objects.
 *                                     Default is 'names'.
 * @return string[]|WP_Taxonomy[] List of taxonomies or taxonomy names. Empty array on failure.
 */
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 = wp_basename($file);
    $objects = array('attachment');
    if (str_contains($filename, '.')) {
        $objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1);
    }
    if (!empty($attachment->post_mime_type)) {
        $objects[] = 'attachment:' . $attachment->post_mime_type;
        if (str_contains($attachment->post_mime_type, '/')) {
            foreach (explode('/', $attachment->post_mime_type) as $token) {
                if (!empty($token)) {
                    $objects[] = "attachment:{$token}";
                }
            }
        }
    }
    $taxonomies = array();
    foreach ($objects as $object) {
        $taxes = get_object_taxonomies($object, $output);
        if ($taxes) {
            $taxonomies = array_merge($taxonomies, $taxes);
        }
    }
    if ('names' === $output) {
        $taxonomies = array_unique($taxonomies);
    }
    return $taxonomies;
}

WordPress Version: 5.4

/**
 * Retrieves taxonomies attached to given the attachment.
 *
 * @since 2.5.0
 * @since 4.7.0 Introduced the `$output` parameter.
 *
 * @param int|array|object $attachment Attachment ID, data array, or data object.
 * @param string           $output     Output type. 'names' to return an array of taxonomy names,
 *                                     or 'objects' to return an array of taxonomy objects.
 *                                     Default is 'names'.
 * @return string[]|WP_Taxonomy[] List of taxonomies or taxonomy names. Empty array on failure.
 */
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 = wp_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) {
        $taxes = get_object_taxonomies($object, $output);
        if ($taxes) {
            $taxonomies = array_merge($taxonomies, $taxes);
        }
    }
    if ('names' === $output) {
        $taxonomies = array_unique($taxonomies);
    }
    return $taxonomies;
}

WordPress Version: 5.3

/**
 * Retrieves taxonomies attached to given the attachment.
 *
 * @since 2.5.0
 * @since 4.7.0 Introduced the `$output` parameter.
 *
 * @param int|array|object $attachment Attachment ID, data array, or data object.
 * @param string           $output     Output type. 'names' to return an array of taxonomy names,
 *                                     or 'objects' to return an array of taxonomy objects.
 *                                     Default is 'names'.
 * @return array Empty array on failure. List of taxonomies on success.
 */
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 = wp_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) {
        $taxes = get_object_taxonomies($object, $output);
        if ($taxes) {
            $taxonomies = array_merge($taxonomies, $taxes);
        }
    }
    if ('names' === $output) {
        $taxonomies = array_unique($taxonomies);
    }
    return $taxonomies;
}

WordPress Version: 5.2

/**
 * Retrieves taxonomies attached to given the attachment.
 *
 * @since 2.5.0
 * @since 4.7.0 Introduced the `$output` parameter.
 *
 * @param int|array|object $attachment Attachment ID, data array, or data object.
 * @param string           $output     Output type. 'names' to return an array of taxonomy names,
 *                                     or 'objects' to return an array of taxonomy objects.
 *                                     Default is 'names'.
 * @return array Empty array on failure. List of taxonomies on success.
 */
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 = wp_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;
}

WordPress Version: 4.7

/**
 * Retrieves taxonomies attached to given the attachment.
 *
 * @since 2.5.0
 * @since 4.7.0 Introduced the `$output` parameter.
 *
 * @param int|array|object $attachment Attachment ID, data array, or data object.
 * @param string           $output     Output type. 'names' to return an array of taxonomy names,
 *                                     or 'objects' to return an array of taxonomy objects.
 *                                     Default is 'names'.
 * @return array Empty array on failure. List of taxonomies on success.
 */
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;
}

WordPress Version: 4.4

/**
 * Retrieves taxonomies attached to given the attachment.
 *
 * @since 2.5.0
 *
 * @param int|array|object $attachment Attachment ID, data array, or data object.
 * @return array Empty array on failure. List of taxonomies on success.
 */
function get_attachment_taxonomies($attachment)
{
    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)) {
            $taxonomies = array_merge($taxonomies, $taxes);
        }
    }
    return array_unique($taxonomies);
}

WordPress Version: 4.2

/**
 * Retrieves taxonomies attached to given the attachment.
 *
 * @since 2.5.0
 *
 * @param int|array|object $attachment Attachment ID, data array, or data object.
 * @return array Empty array on failure. List of taxonomies on success.
 */
function get_attachment_taxonomies($attachment)
{
    if (is_int($attachment)) {
        $attachment = get_post($attachment);
    } elseif (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);
}

WordPress Version: 3.7

/**
 * Retrieve taxonomies attached to the attachment.
 *
 * @since 2.5.0
 *
 * @param int|array|object $attachment Attachment ID, Attachment data array, or Attachment data object.
 * @return array Empty array on failure. List of taxonomies on success.
 */
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);
}