The WPFolks app is out on iPhone and Android. Free, no ads. Download on the App Store Get it on Google Play
Skip to main content
Login Join
Snippet · PHP

Display Media Attachment Count in the WordPress Pages List

Shared by Alkesh Miyani · October 29, 2026 · @manage_pages_custom_column

18 views
Back to Snippets

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

  1. Add the code to your child theme’s functions.php file or a PHP snippets plugin.
  2. Navigate to WordPress Admin → Pages → All Pages.
  3. Find the new Media Attachments column.
  4. 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

/**
 * 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

WordPress 7.1.4 WordPress Admin

Reported by the author on October 29, 2026. WPFolks has not verified this.

Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

1 comment

  1. Amit Dholiya
    Amit Dholiya @amitdholiya

    Useful snippet for auditing media on a large site. The Media Attachments column in the Pages list makes it easy to see which pages have the most uploads. Good to note that it counts attachments whose parent is the page, not every file embedded in the content, so the numbers may differ from what’s actually shown on the page.

    Report

Proudly supported by