How to Display Post Attachment Count in the Admin Column in WordPress

Home » Snippets » How to Display Post Attachment Count in the Admin Column in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you’re running a WordPress site that relies heavily on images or other attachments, it’s important to keep track of the number of attachments associated with each post. However, by default, WordPress doesn’t display this information in the admin column. In this article, we’ll show you how to add a custom column to the post listing table that will display the attachment count for each post, making it easier to manage and organize your media.

					function wpturbo_add_attachment_count_column_header( $defaults ) {
    $defaults['wpturbo_attachment_count'] = __( 'Attachments', 'wpturbo' );
    return $defaults;
}

function wpturbo_add_attachment_count_column_content( $column_name, $post_id ) {
    if ( $column_name == 'wpturbo_attachment_count' ) {
        $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment' ) );
        $count = count( $attachments );
        echo '<div class="wpturbo_attachment_count_column">' . $count . '</div>';
    }
}

add_filter( 'manage_posts_columns', 'wpturbo_add_attachment_count_column_header' );
add_action( 'manage_posts_custom_column', 'wpturbo_add_attachment_count_column_content', 10, 2 );
				

In this article, we will discuss how to display the attachment count of a post in the WordPress admin column. By default, WordPress does not show the attachment count of a post in the admin column.

We will use the manage_posts_columns filter to add a new column header labeled "Attachments" to the admin panel.

function wpturbo_add_attachment_count_column_header( $defaults ) {
    $defaults['wpturbo_attachment_count'] = __( 'Attachments', 'wpturbo' );
    return $defaults;
}
add_filter( 'manage_posts_columns', 'wpturbo_add_attachment_count_column_header' );

The wpturbo_add_attachment_count_column_header function defines a new column label called "Attachments," which will be added to the header of our new column.

Similarly, we will use the manage_posts_custom_column action hook to add content to our newly created column. We will utilize the $column_name and $post_id variables to check if the current column is our custom "Attachments" column and retrieve the count of attachments associated with the post.

function wpturbo_add_attachment_count_column_content( $column_name, $post_id ) {
    if ( $column_name == 'wpturbo_attachment_count' ) {
        $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment' ) );
        $count = count( $attachments );
        echo '<div class="wpturbo_attachment_count_column">' . $count . '</div>';
    }
}
add_action( 'manage_posts_custom_column', 'wpturbo_add_attachment_count_column_content', 10, 2 );

Inside the wpturbo_add_attachment_count_column_content function, we first check if the $column_name variable matches our custom "Attachments" column. If it does, we use the get_children function with the post_parent parameter set to the $post_id variable and the post_type parameter set to "attachment" to retrieve all attachments associated with the current post.

We then use the count function to calculate the number of attachments and output the count to our new column with the echo statement.

At this point, we have created a new column in the WordPress admin panel that displays the attachment count of each post.

Register an account to save your snippets or go Pro to get more features.