• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / get_attachment_taxonomies() – Retrieves taxonomies attached to given the attachment.

You appear to be a bot. Output may be restricted

Description

Retrieves taxonomies attached to given the attachment.

Usage

$string[]|WP_Taxonomy[] = get_attachment_taxonomies( $attachment, $output );

Parameters

$attachment
( int|array|object ) required – Attachment ID, data array, or data object.
$output
( string ) optional default: names – Output type. 'names' to return an array of taxonomy names, or 'objects' to return an array of taxonomy objects. Default is 'names'.

Returns

string[]|WP_Taxonomy[] List of taxonomies or taxonomy names. Empty array on failure.

Source

File name: wordpress/wp-includes/media.php
Lines:

1 to 48 of 48
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;
}
 

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 25th November 2019

Primary Sidebar

Information

Function name: get_attachment_taxonomies
Plugin ref: WordPress
Version: 5.6
Sourcefile: wp-includes/media.php
File ref: wp-includes/media.php
Deprecated?: No
API Letters: A,G,T

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites