WordPress Snippets at WPcustoms

Show attachment count in post table

On image heavy blogs your posts may contain many attachments. This function displays the attachment count in the admin post table.


/**
 * Snippet Name: Show attachment count in post table
 * Snippet URL: http://www.wpcustoms.net/snippets/show-attachment-count-post-table/
 */
 function wpc_posts_columns_attachment_count($defaults){
    $defaults['wps_post_attachments'] = __('Attachments');
    return $defaults;
}
function wpc_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;}
    }
}
add_filter('manage_posts_columns', 'wpc_posts_columns_attachment_count', 5);
add_action('manage_posts_custom_column', 'wpc_posts_custom_columns_attachment_count', 5, 2);