# "Sort by SKU" Option in the Shop

> Adds a "Sort by product code" choice to the shop's sorting dropdown, useful for trade catalogues where customers order by code.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, sorting, sku, catalog
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-sort-by-sku/

## Code

```php
add_filter( 'woocommerce_catalog_orderby', function ( $options ) {
    $options['sku'] = 'Sort by product code';

    return $options;
} );

add_filter( 'woocommerce_get_catalog_ordering_args', function ( $args, $orderby ) {
    if ( 'sku' === $orderby ) {
        $args['orderby']  = 'meta_value title';
        $args['order']    = 'ASC';
        $args['meta_key'] = '_sku'; // phpcs:ignore WordPress.DB.SlowDBQuery
    }

    return $args;
}, 10, 2 );
```

## Notes

Products without a SKU are listed last.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find ""Sort by SKU" Option in the Shop" 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/
