get_attached_media

Retrieves media attached to the passed post.

Description

(array) get_attached_media( (string) $type, (int) $post = 0 ); 

Returns (array)

Found attachments.

Parameters (2)

0. $type (string)
Mime type.
1. $post — Optional. (int)
Post ID or WP_Post object. Default is global $post.

Usage

  1. if ( !function_exists( 'get_attached_media' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Mime type. 
  5. $type = ''; 
  6.  
  7. // Optional. Post ID or WP_Post object. Default is global $post. 
  8. $post = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_attached_media($type, $post); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/media.php  
  1. function get_attached_media( $type, $post = 0 ) { 
  2. if ( ! $post = get_post( $post ) ) 
  3. return array(); 
  4.  
  5. $args = array( 
  6. 'post_parent' => $post->ID,  
  7. 'post_type' => 'attachment',  
  8. 'post_mime_type' => $type,  
  9. 'posts_per_page' => -1,  
  10. 'orderby' => 'menu_order',  
  11. 'order' => 'ASC',  
  12. ); 
  13.  
  14. /** 
  15. * Filters arguments used to retrieve media attached to the given post. 
  16. * @since 3.6.0 
  17. * @param array $args Post query arguments. 
  18. * @param string $type Mime type of the desired media. 
  19. * @param mixed $post Post ID or object. 
  20. */ 
  21. $args = apply_filters( 'get_attached_media_args', $args, $type, $post ); 
  22.  
  23. $children = get_children( $args ); 
  24.  
  25. /** 
  26. * Filters the list of media attached to the given post. 
  27. * @since 3.6.0 
  28. * @param array $children Associative array of media attached to the given post. 
  29. * @param string $type Mime type of the media desired. 
  30. * @param mixed $post Post ID or object. 
  31. */ 
  32. return (array) apply_filters( 'get_attached_media', $children, $type, $post );