X

How To Display a Post Attachment Count in an Admin Column

Snippets by IsItWP

Ever wanted to display a post attachment count in an admin column? This snippet will display the number of post attachments for each post in a custom admin column.

Instructions

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

add_filter('manage_posts_columns', 'posts_columns_attachment_count', 5);
add_action('manage_posts_custom_column', 'posts_custom_columns_attachment_count', 5, 2);

function posts_columns_attachment_count($defaults){
    $defaults['wps_post_attachments'] = __('Attached');
    return $defaults;
}
function posts_custom_columns_attachment_count($column_name, $id){
	if($column_name === 'wps_post_attachments'){
	$attachments = get_children(array('post_parent'=>$id));
	$count = count($attachments);
	if($count !=0){echo $count;}
    }
}

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out How to Set Up Download Tracking in WordPress With Google Analytics.

Comments   Leave a Reply

  1. how can i implement it for custom post type?

  2. 16 Useful Code Snippets for WordPress September 9, 2015 at 8:02 am

    […] Source: WPSNIPP […]

  3. Very cool!

    1. glad to see you like the snippet!

  4. How to display post attachment count in admin post list January 14, 2013 at 10:17 am

    […] to Kevin Chard for the […]

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!