wppaste
WordPress

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:3975
Retrieves taxonomies attached to given the attachment.

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

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
9–61

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 84.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
!is_int($attachment) && !is_object($attachment)9–11none
is_int($attachment) && !is_object($attachment)12get_post()
!is_int($attachment) && is_object($attachment) && !$filename && $output !== "names"29–39get_attached_file(), wp_basename()
is_int($attachment) && is_object($attachment) && !$filename && $output !== "names"32–40get_post(), get_attached_file(), wp_basename()
!is_int($attachment) && is_object($attachment) && !$filename && $output === "names"33–43get_attached_file(), wp_basename(), array_unique()
is_int($attachment) && is_object($attachment) && !$filename && $output === "names"36–44get_post(), get_attached_file(), wp_basename(), array_unique()
!is_int($attachment) && is_object($attachment) && $filename && $output !== "names"38–48get_attached_file(), wp_basename(), strrpos()
is_int($attachment) && is_object($attachment) && $filename && $output !== "names"41–49get_post(), get_attached_file(), wp_basename(), strrpos()
!is_int($attachment) && is_object($attachment) && $filename && $output === "names"42–52get_attached_file(), wp_basename(), strrpos(), array_unique()
!is_int($attachment) && is_object($attachment) && !$filename && !empty($attachment) && $output !== "names"43–47get_attached_file(), wp_basename(), explode()
is_int($attachment) && is_object($attachment) && $filename && $output === "names"45–53get_post(), get_attached_file(), wp_basename(), strrpos(), array_unique()
is_int($attachment) && is_object($attachment) && !$filename && !empty($attachment) && $output !== "names"46–48get_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–51get_attached_file(), wp_basename(), explode(), array_unique()
is_int($attachment) && is_object($attachment) && !$filename && !empty($attachment) && $output === "names"50–52get_post(), get_attached_file(), wp_basename(), explode(), array_unique()
!is_int($attachment) && is_object($attachment) && $filename && !empty($attachment) && $output !== "names"52–56get_attached_file(), wp_basename(), strrpos(), explode()
is_int($attachment) && is_object($attachment) && $filename && !empty($attachment) && $output !== "names"55–57get_post(), get_attached_file(), wp_basename(), strrpos(), explode()
!is_int($attachment) && is_object($attachment) && $filename && !empty($attachment) && $output === "names"56–60get_attached_file(), wp_basename(), strrpos(), explode(), array_unique()
is_int($attachment) && is_object($attachment) && $filename && !empty($attachment) && $output === "names"59–61get_post(), get_attached_file(), wp_basename(), strrpos(), explode(), array_unique()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev849–6113
8.5849–6113
8.4849–61139 fewer instructions than PHP 8.3
8.3939–7013
8.2939–7013
8.1939–70131 fewer instruction than PHP 7.4
7.4949–7113

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

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

4.7.0
Introduced the $output parameter.from the docblock
2.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 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.