Retrieves media attached to the passed post.
Parameters
Source
// Edit Audio.
'audioDetailsTitle' => __( 'Audio details' ),
'audioReplaceTitle' => __( 'Replace audio' ),
'audioAddSourceTitle' => __( 'Add audio source' ),
'audioDetailsCancel' => __( 'Cancel edit' ),
// Edit Video.
'videoDetailsTitle' => __( 'Video details' ),
'videoReplaceTitle' => __( 'Replace video' ),
'videoAddSourceTitle' => __( 'Add video source' ),
'videoDetailsCancel' => __( 'Cancel edit' ),
'videoSelectPosterImageTitle' => __( 'Select poster image' ),
'videoAddTrackTitle' => __( 'Add subtitles' ),
// Playlist.
'playlistDragInfo' => __( 'Drag and drop to reorder tracks.' ),
'createPlaylistTitle' => __( 'Create audio playlist' ),
'editPlaylistTitle' => __( 'Edit audio playlist' ),
'cancelPlaylistTitle' => __( '← Cancel audio playlist' ),
'insertPlaylist' => __( 'Insert audio playlist' ),
'updatePlaylist' => __( 'Update audio playlist' ),
'addToPlaylist' => __( 'Add to audio playlist' ),
'addToPlaylistTitle' => __( 'Add to Audio Playlist' ),
// Video Playlist.
'videoPlaylistDragInfo' => __( 'Drag and drop to reorder videos.' ),
'createVideoPlaylistTitle' => __( 'Create video playlist' ),
'editVideoPlaylistTitle' => __( 'Edit video playlist' ),
'cancelVideoPlaylistTitle' => __( '← Cancel video playlist' ),
'insertVideoPlaylist' => __( 'Insert video playlist' ),
'updateVideoPlaylist' => __( 'Update video playlist' ),
'addToVideoPlaylist' => __( 'Add to video playlist' ),
'addToVideoPlaylistTitle' => __( 'Add to video Playlist' ),
// Headings.
'filterAttachments' => __( 'Filter media' ),
'attachmentsList' => __( 'Media list' ),
);
/**
Changelog
Version | Description |
---|---|
3.6.0 | Introduced. |
You can get all attached media, regardless of type, by passing an empty string:
or
Important to note that this function only returns the attachments that were first uploaded/added to the post.
Uploading an image to Post A(ID 1) and then adding that image later to Post B(ID 2) would give an empty array if the following code was used:
$media = get_attached_media( 'image', 2 );
var_dump( $media );
You’d only get array data if you upload your media and add it to Post B before any other post.
It should be noted, that this function returns array of
WP_Post
objects, indexed by their ID, ordered by their “menu order” by default – that is where usually plugins handling attachment store their order positions…Examples
Get image attachment(s) to the current Post:
Get attachment(s) of mime-type ‘audio’ to the Post with an ID of 102: