How To Display Post Attachment Count In WordPress Posts Screen

Some people run WordPress websites where they post articles with multiple attachments and here’s something useful for them. Here’s snippet which allow yous to display post attachment count in WordPress posts screen, so you can easily see the number of media attached to a post.

Just add following snippet to your current theme’s functions.php file:

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

This snippet only works for posts, but you can use it for pages by adding following snippet:

add_filter('manage_pages_columns', 'posts_columns_attachment_count', 5);
add_action('manage_pages_custom_column', 'posts_custom_columns_attachment_count', 5, 2);

Thanks to WPSnipp.com for the snippet.

Leave a Comment