Retrieves media attached to the passed post.
Parameters
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
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: