# Sortable Total Sales Column for Products

> Adds a "Sales" column to the admin products list showing how many units each product has sold, and lets you sort by it.

- Type: PHP
- Runs: Admin only
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, admin, products, sales
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-products-sales-column/

## Code

```php
add_filter( 'manage_edit-product_columns', function ( $columns ) {
    $columns['wpide_sales'] = 'Sales';

    return $columns;
}, 20 );

add_action( 'manage_product_posts_custom_column', function ( $column, $post_id ) {
    if ( 'wpide_sales' === $column ) {
        $product = wc_get_product( $post_id );
        echo $product ? (int) $product->get_total_sales() : 0;
    }
}, 10, 2 );

add_filter( 'manage_edit-product_sortable_columns', function ( $columns ) {
    $columns['wpide_sales'] = 'wpide_sales';

    return $columns;
} );

add_action( 'pre_get_posts', function ( $query ) {
    if ( is_admin() && $query->is_main_query() && 'wpide_sales' === $query->get( 'orderby' ) ) {
        $query->set( 'meta_key', 'total_sales' );
        $query->set( 'orderby', 'meta_value_num' );
    }
} );
```

## Notes

Uses the total_sales counter WooCommerce keeps for each product.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Sortable Total Sales Column for Products" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
