This code modifies the product categories page in the WooCommerce admin area. Here’s what it does :
- Adds a custom column called “All” to the product categories table
- Populates the custom column with a count that includes multiple product statuses.
Key features :
- Adds a the custom count column named “All”
- Counts products in three statuses: published, draft, and pending
- Makes the count clickable, linking to a filtered view of products
- Uses WP_Query to get accurate counts for each status
- Properly escapes URLs and integers for security
Restrictions
- Only handles categories (not tags)
- Only works in the admin area (not frontend)
- Has hardcoded statuses (not configurable)
- Uses a different approach to counting (WP_Query instead of direct SQL)
add_filter('manage_edit-product_cat_columns', 'custom_product_cat_columns');
function custom_product_cat_columns($columns) {
$columns['custom_count'] = __('All', 'woocommerce');
unset($columns['count']); // Remove default count column
return $columns;
}
add_filter('manage_product_cat_custom_column', 'custom_product_cat_column_content', 10, 3);
function custom_product_cat_column_content($content, $column, $term_id) {
if ($column === 'custom_count') {
$statuses = ['publish', 'draft', 'pending'];
$count = 0;
foreach ($statuses as $status) {
$query = new WP_Query([
'post_type' => 'product',
'post_status' => $status,
'tax_query' => [
[
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $term_id,
]
],
'fields' => 'ids',
'posts_per_page' => -1,
]);
$count += $query->found_posts;
}
// Link to filtered products by term ID
$term = get_term($term_id, 'product_cat');
$url = admin_url('edit.php?post_type=product&product_cat=' . $term->slug);
$content = '<a href="' . esc_url($url) . '">' . intval($count) . '</a>';
}
return $content;
}
Need a more flexible solution which doesn’t require custom coding? Try :






Leave a Reply