# Media Library Overview Widget

> Adds a dashboard widget breaking the Media Library down into images, videos, audio and documents, with uploads this month and the number of files not attached to any content.

- Type: PHP
- Runs: Admin only
- Category: [Dashboard](https://wpide.com/library/category/dashboard/)
- Tags: dashboard-widget, media, statistics
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/dash-media-overview-widget/

## Code

```php
add_action( 'wp_dashboard_setup', function () {
    if ( ! current_user_can( 'upload_files' ) ) {
        return;
    }

    wp_add_dashboard_widget( 'wpide_media_overview', 'Media Library overview', function () {
        global $wpdb;

        $stats = get_transient( 'wpide_media_overview' );
        if ( false === $stats ) {
            $types = $wpdb->get_results(
                "SELECT SUBSTRING_INDEX(post_mime_type, '/', 1) AS kind, COUNT(*) AS total FROM {$wpdb->posts} WHERE post_type = 'attachment' GROUP BY kind",
                OBJECT_K
            );
            $stats = array(
                'image'      => (int) ( $types['image']->total ?? 0 ),
                'video'      => (int) ( $types['video']->total ?? 0 ),
                'audio'      => (int) ( $types['audio']->total ?? 0 ),
                'other'      => (int) array_sum( array_map( function ( $row ) {
                    return in_array( $row->kind, array( 'image', 'video', 'audio' ), true ) ? 0 : (int) $row->total;
                }, $types ) ),
                'month'      => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_date >= %s", wp_date( 'Y-m-01 00:00:00' ) ) ),
                'unattached' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_parent = 0" ),
            );
            set_transient( 'wpide_media_overview', $stats, HOUR_IN_SECONDS );
        }

        $tiles = array(
            'Images'    => array( $stats['image'], 'upload.php?post_mime_type=image' ),
            'Videos'    => array( $stats['video'], 'upload.php?post_mime_type=video' ),
            'Audio'     => array( $stats['audio'], 'upload.php?post_mime_type=audio' ),
            'Documents' => array( $stats['other'], 'upload.php?mode=list' ),
        );

        echo '<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(90px,1fr));gap:8px;margin-bottom:12px">';
        foreach ( $tiles as $label => $tile ) {
            printf(
                '<a href="%s" style="display:block;border:1px solid #dcdcde;border-radius:4px;padding:10px;text-align:center;text-decoration:none"><span style="display:block;font-size:22px;font-weight:600;color:#1d2327">%s</span>%s</a>',
                esc_url( admin_url( $tile[1] ) ),
                esc_html( number_format_i18n( $tile[0] ) ),
                esc_html( $label )
            );
        }
        echo '</div>';
        printf(
            '<p>%s uploaded this month &middot; <a href="%s">%s not attached to any content</a></p>',
            esc_html( number_format_i18n( $stats['month'] ) ),
            esc_url( admin_url( 'upload.php?mode=list&attachment-filter=detached' ) ),
            esc_html( number_format_i18n( $stats['unattached'] ) )
        );
    } );
} );

add_action( 'add_attachment', function () {
    delete_transient( 'wpide_media_overview' );
} );
```

## Notes

Counts are cached for an hour; the "unattached" link opens the matching Media Library view.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Media Library Overview Widget" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
