# Attached Media Count Column

> Adds a Media column to the Posts and Pages lists with the number of files uploaded to each item, linking to them in the Media Library, to spot heavy or image-less content.

- Type: PHP
- Runs: Admin only
- Category: [Admin](https://wpide.com/library/category/admin/)
- Tags: admin-columns, media, attachments
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/admin-attached-media-count-column/

## Code

```php
$wpide_media_counts = null;

$wpide_media_count_cell = function ( $column, $post_id ) use ( &$wpide_media_counts ) {
    if ( 'wpide_media_count' !== $column ) {
        return;
    }

    // One query for every row on the screen.
    if ( null === $wpide_media_counts ) {
        global $wpdb, $wp_query;
        $ids               = $wp_query && $wp_query->posts ? wp_list_pluck( $wp_query->posts, 'ID' ) : array( $post_id );
        $ids               = array_map( 'absint', $ids );
        $wpide_media_counts = wp_list_pluck( $wpdb->get_results(
            "SELECT post_parent, COUNT(*) AS total FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_parent IN (" . implode( ',', $ids ? $ids : array( 0 ) ) . ') GROUP BY post_parent'
        ), 'total', 'post_parent' );
    }

    $count = (int) ( $wpide_media_counts[ $post_id ] ?? 0 );
    echo $count
        ? '<a href="' . esc_url( admin_url( 'upload.php?mode=list&attachment-filter=all&post_parent=' . $post_id ) ) . '">' . (int) $count . '</a>'
        : '0';
};

foreach ( array( 'post', 'page' ) as $wpide_type ) {
    add_filter( "manage_{$wpide_type}_posts_columns", function ( $columns ) {
        $columns['wpide_media_count'] = '<span class="dashicons dashicons-format-gallery" aria-hidden="true"></span><span class="screen-reader-text">Media</span>';
        return $columns;
    } );
    add_action( "manage_{$wpide_type}_posts_custom_column", $wpide_media_count_cell, 10, 2 );
}

add_action( 'admin_head-edit.php', function () {
    echo '<style>.column-wpide_media_count{width:4em;text-align:center}</style>';
} );
```

## Notes

Counts files attached through "Uploaded to"; images picked from the library after upload are not attached.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Attached Media Count Column" 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/
