On this page
get_attached_media( string $type, int|WP_Post $post ): WP_Post[]
Retrieves media attached to the passed post.
Parameters
Return
WP_Post[] Array of media attached to the given post.
Source
File: wp-includes/media.php. View all references
function get_attached_media( $type, $post = 0 ) {
$post = get_post( $post );
if ( ! $post ) {
return array();
}
$args = array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => $type,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
);
/**
* Filters arguments used to retrieve media attached to the given post.
*
* @since 3.6.0
*
* @param array $args Post query arguments.
* @param string $type Mime type of the desired media.
* @param WP_Post $post Post object.
*/
$args = apply_filters( 'get_attached_media_args', $args, $type, $post );
$children = get_children( $args );
/**
* Filters the list of media attached to the given post.
*
* @since 3.6.0
*
* @param WP_Post[] $children Array of media attached to the given post.
* @param string $type Mime type of the media desired.
* @param WP_Post $post Post object.
*/
return (array) apply_filters( 'get_attached_media', $children, $type, $post );
}
Hooks
- apply_filters( 'get_attached_media',
WP_Post[] $children ,string $type ,WP_Post $post ) -
Filters the list of media attached to the given post.
- apply_filters( 'get_attached_media_args',
array $args ,string $type ,WP_Post $post ) -
Filters arguments used to retrieve media attached to the given post.
Related
Uses
| Uses | Description |
|---|---|
| get_children() wp-includes/post.php | Retrieves all children of the post parent ID. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
Used By
| Used By | Description |
|---|---|
| wp_video_shortcode() wp-includes/media.php | Builds the Video shortcode output. |
| wp_audio_shortcode() wp-includes/media.php | Builds the Audio shortcode output. |
Changelog
| Version | Description |
|---|---|
| 3.6.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_attached_media