get_attachment_taxonomies( int|array|object $attachment, string $output = 'names' ): string[]|WP_Taxonomy[]
- Since
- 2.5.0, 4.7.0
- Source
wp-includes/media.php:4164
Compatibility
- WordPress
- since 4.7.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$attachmentint|array|object- Attachment ID, data array, or data object.
$outputstringoptional- Output type. 'names' to return an array of taxonomy names, or 'objects' to return an array of taxonomy objects.
Default is 'names'.Default:'names'
Return value
string[]|WP_Taxonomy[]- List of taxonomies or taxonomy names. Empty array on failure.
Performance profile
How much work a call to get_attachment_taxonomies() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 9–61
- Plugin surface
- None
- Called by
- 5
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 84.
Nothing here hands control to plugin code.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()one call below get_attachment_taxonomies()
Further down the call graph this can also reach cache, serialize, option and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 18 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_attachment_taxonomies() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_int($attachment) && !is_object($attachment) | 9–11 | none |
is_int($attachment) && !is_object($attachment) | 12 | get_post() |
!is_int($attachment) && is_object($attachment) && !$filename && $output !== "names" | 29–39 | get_attached_file(), wp_basename() |
is_int($attachment) && is_object($attachment) && !$filename && $output !== "names" | 32–40 | get_post(), get_attached_file(), wp_basename() |
!is_int($attachment) && is_object($attachment) && !$filename && $output === "names" | 33–43 | get_attached_file(), wp_basename(), array_unique() |
is_int($attachment) && is_object($attachment) && !$filename && $output === "names" | 36–44 | get_post(), get_attached_file(), wp_basename(), array_unique() |
!is_int($attachment) && is_object($attachment) && $filename && $output !== "names" | 38–48 | get_attached_file(), wp_basename(), strrpos() |
is_int($attachment) && is_object($attachment) && $filename && $output !== "names" | 41–49 | get_post(), get_attached_file(), wp_basename(), strrpos() |
!is_int($attachment) && is_object($attachment) && $filename && $output === "names" | 42–52 | get_attached_file(), wp_basename(), strrpos(), array_unique() |
!is_int($attachment) && is_object($attachment) && !$filename && !empty($attachment) && $output !== "names" | 43–47 | get_attached_file(), wp_basename(), explode() |
is_int($attachment) && is_object($attachment) && $filename && $output === "names" | 45–53 | get_post(), get_attached_file(), wp_basename(), strrpos(), array_unique() |
is_int($attachment) && is_object($attachment) && !$filename && !empty($attachment) && $output !== "names" | 46–48 | get_post(), get_attached_file(), wp_basename(), explode() |
6 further outcomes, up to 61 instructions
!is_int($attachment) && is_object($attachment) && !$filename && !empty($attachment) && $output === "names" | 47–51 | get_attached_file(), wp_basename(), explode(), array_unique() |
is_int($attachment) && is_object($attachment) && !$filename && !empty($attachment) && $output === "names" | 50–52 | get_post(), get_attached_file(), wp_basename(), explode(), array_unique() |
!is_int($attachment) && is_object($attachment) && $filename && !empty($attachment) && $output !== "names" | 52–56 | get_attached_file(), wp_basename(), strrpos(), explode() |
is_int($attachment) && is_object($attachment) && $filename && !empty($attachment) && $output !== "names" | 55–57 | get_post(), get_attached_file(), wp_basename(), strrpos(), explode() |
!is_int($attachment) && is_object($attachment) && $filename && !empty($attachment) && $output === "names" | 56–60 | get_attached_file(), wp_basename(), strrpos(), explode(), array_unique() |
is_int($attachment) && is_object($attachment) && $filename && !empty($attachment) && $output === "names" | 59–61 | get_post(), get_attached_file(), wp_basename(), strrpos(), explode(), array_unique() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 84 | 9–61 | 13 | |
| 8.5 | 84 | 9–61 | 13 | |
| 8.4 | 84 | 9–61 | 13 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 93 | 9–70 | 13 | |
| 8.2 | 93 | 9–70 | 13 | |
| 8.1 | 93 | 9–70 | 13 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 94 | 9–71 | 13 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 5
- get_post()Retrieves post data given a post ID or post object.
- get_attached_file()Retrieves attached file path based on attachment ID.
- wp_basename()i18n-friendly version of basename().
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- get_object_taxonomies()Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name.
Used by · 5
- get_attachment_fields_to_edit()Retrieves the attachment fields to edit form fields.
- get_compat_media_markup()Retrieves the media markup for an attachment.
- get_object_taxonomies()Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name.
- media_upload_form_handler()Handles form submissions for the legacy media uploader.
- wp_ajax_save_attachment_compat()Handles saving backward compatible attachment attributes via AJAX.
Source code
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;}Changelog
Introduced in 4.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$output parameter.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/media.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.