get_attached_media( string $type, int|WP_Post $post ): WP_Post[]

Retrieves media attached to the passed post.

Parameters

$typestringrequired
Mime type.
$postint|WP_Postoptional
Post ID or WP_Post object. Default is global $post.

Return

WP_Post[] Array of media attached to the given post.

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

VersionDescription
3.6.0Introduced.

User Contributed Notes

  1. Skip to note 6 content

    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.

You must log in before being able to contribute a note or feedback.