When managing a WordPress website with many pages, it can be useful to know how many media files have been uploaded to each page.
This snippet adds a Media Attachments column to the WordPress admin Pages list and displays the number of media files attached to each page.
How to use
- Add the code to your child theme’s
functions.phpfile or a PHP snippets plugin. - Navigate to WordPress Admin → Pages → All Pages.
- Find the new Media Attachments column.
- Review the attachment count displayed for each page.
Example output
| Page | Media Attachments |
|---|---|
| About Us | 4 files |
| Contact | 2 files |
| Services | 0 files |
Example data only.
Notes
- Counts media attachments of all file types, including images, videos, audio, and documents.
- Counts attachments whose WordPress parent is the relevant page.
- Does not count every media file embedded in page content.
- Does not modify pages or media files.
- Uses WordPress APIs and escaped output.
/**
* Add a Media Attachments column to the Pages list.
*/
add_filter(
'manage_pages_columns',
'alkesh_add_media_attachments_column'
);
function alkesh_add_media_attachments_column( $columns ) {
$columns['media_attachments'] = __( 'Media Attachments', 'alkesh' );
return $columns;
}
/**
* Display the number of attached media files.
*/
add_action(
'manage_pages_custom_column',
'alkesh_display_media_attachments_column',
10,
2
);
function alkesh_display_media_attachments_column( $column_name, $post_id ) {
static $attachment_counts = array();
if ( 'media_attachments' !== $column_name ) {
return;
}
if ( ! isset( $attachment_counts[ $post_id ] ) ) {
$attachment_ids = get_posts(
array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => -1,
'fields' => 'ids',
'no_found_rows' => true,
)
);
$attachment_counts[ $post_id ] = count( $attachment_ids );
}
printf(
'<span>%s</span>',
esc_html(
sprintf(
_n(
'%d file',
'%d files',
$attachment_counts[ $post_id ],
'alkesh'
),
$attachment_counts[ $post_id ]
)
)
);
}
Tested on
Reported by the author on October 29, 2026. WPFolks has not verified this.
