X

Display post attachment count in admin column

Sometimes you have a post with multiple attachments and it would be nice to see a count of the total media attached to a post. Adding this snippet to the functions.php of your wordpress theme will display the post attachment count in a custom admin column.


( example screenshot )


Code Snippet:


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;}
}
}

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!