Query the media library with WP_Query
Every uploaded file is a post of the type attachment with the status inherit, the file’s MIME type in post_mime_type and the post it was uploaded to in post_parent. So the media library can be queried with WP_Query like any other content: all PDFs, the images of one post, the uploads of the last month, files without a parent.
Problem: A page should list all PDF documents of the site, and a gallery should show all images uploaded to the current post, without a gallery plugin.
Solution: Query with 'post_type' => 'attachment' and 'post_status' => 'inherit', filter with post_mime_type and post_parent, and output the files with wp_get_attachment_url() and wp_get_attachment_image(). For one post, get_attached_media() is a shortcut.
All PDFs
<?php
$pdfs = new WP_Query( array(
'post_type' => 'attachment',
'post_status' => 'inherit', // attachments never have "publish"
'post_mime_type' => 'application/pdf',
'posts_per_page' => 20,
'orderby' => 'date',
'order' => 'DESC',
) );
if ( $pdfs->have_posts() ) {
echo '<ul class="documents">';
while ( $pdfs->have_posts() ) {
$pdfs->the_post();
printf(
'<li><a href="%s">%s</a> (%s)</li>',
esc_url( wp_get_attachment_url( get_the_ID() ) ),
esc_html( get_the_title() ),
esc_html( size_format( filesize( get_attached_file( get_the_ID() ) ) ) )
);
}
echo '</ul>';
wp_reset_postdata();
}
Without post_status the query looks for published posts and finds nothing. post_mime_type accepts a full type (image/jpeg), a group (image, video) or an array of them.
The images of one post
<?php
$images = new WP_Query( array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_parent' => get_the_ID(),
'post_mime_type' => 'image',
'posts_per_page' => -1,
'orderby' => 'menu_order ID',
'order' => 'ASC',
'no_found_rows' => true,
) );
The shortcut for the same thing:
<?php
foreach ( get_attached_media( 'image', get_the_ID() ) as $image ) {
echo wp_get_attachment_image( $image->ID, 'medium' );
}
“Attached” means “uploaded while editing this post”. An image inserted into the post from the library, or uploaded somewhere else first, has another parent or none. For galleries chosen by the editor, the IDs in the gallery block are the reliable source, not post_parent.
Recent and unattached files
<?php
// Images uploaded in the last 30 days
$recent = new WP_Query( array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'image',
'date_query' => array( array( 'after' => '30 days ago' ) ),
'posts_per_page' => 50,
) );
// Files without a parent post
$unattached = new WP_Query( array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_parent' => 0,
'posts_per_page' => 100,
'fields' => 'ids',
) );
NOTE: “Unattached” does not mean “unused”. Images used as featured images, in widgets, in theme options, in the Customizer or inserted into posts from the library are often unattached. Never bulk-delete files only because post_parent is 0; check where an image is used first, and keep a backup of the uploads folder.