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

 * lack of plural support here, turn it into "selected: %d" then translate it.
 */
'selected'                    => __( '%d selected' ),
'dragInfo'                    => __( 'Drag and drop to reorder media files.' ),

// Upload.
'uploadFilesTitle'            => __( 'Upload files' ),
'uploadImagesTitle'           => __( 'Upload images' ),

// Library.
'mediaLibraryTitle'           => __( 'Media Library' ),
'insertMediaTitle'            => __( 'Add media' ),
'createNewGallery'            => __( 'Create a new gallery' ),
'createNewPlaylist'           => __( 'Create a new playlist' ),
'createNewVideoPlaylist'      => __( 'Create a new video playlist' ),
'returnToLibrary'             => __( '← Go to library' ),
'allMediaItems'               => __( 'All media items' ),
'allDates'                    => __( 'All dates' ),
'noItemsFound'                => __( 'No items found.' ),
'insertIntoPost'              => $post_type_object->labels->insert_into_item,
'unattached'                  => _x( 'Unattached', 'media items' ),
'mine'                        => _x( 'Mine', 'media items' ),
'trash'                       => _x( 'Trash', 'noun' ),
'uploadedToThisPost'          => $post_type_object->labels->uploaded_to_this_item,
'warnDelete'                  => __( "You are about to permanently delete this item from your site.\nThis action cannot be undone.\n 'Cancel' to stop, 'OK' to delete." ),
'warnBulkDelete'              => __( "You are about to permanently delete these items from your site.\nThis action cannot be undone.\n 'Cancel' to stop, 'OK' to delete." ),
'warnBulkTrash'               => __( "You are about to trash these items.\n  'Cancel' to stop, 'OK' to delete." ),
'bulkSelect'                  => __( 'Bulk select' ),
'trashSelected'               => __( 'Move to Trash' ),
'restoreSelected'             => __( 'Restore from Trash' ),
'deletePermanently'           => __( 'Delete permanently' ),
'errorDeleting'               => __( 'Error in deleting the attachment.' ),
'apply'                       => __( 'Apply' ),
'filterByDate'                => __( 'Filter by date' ),
'filterByType'                => __( 'Filter by type' ),
'searchLabel'                 => __( 'Search media' ),
'searchMediaLabel'            => __( 'Search media' ),          // Backward compatibility pre-5.3.
'searchMediaPlaceholder'      => __( 'Search media items...' ), // Placeholder (no ellipsis), backward compatibility pre-5.3.
/* translators: %d: Number of attachments found in a search. */
'mediaFound'                  => __( 'Number of media items found: %d' ),

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.