Home » WooCommerce: Sort Products By Stock @ Admin Dashboard

WooCommerce: Sort Products By Stock @ Admin Dashboard

by Tutor Aspire

We’ve already seen how to sort frontend products by in stock first and out of stock last, but what about doing the exact same for administrators and shop managers in the backend?

Sometimes shops have to deal with a lot of “sold” products and the products table becomes messy, so this is a way to sort by stock status (In stock -> Out of stock) on load. You can then use the column headers to sort by other data as usual, so this will only work once you access /wp-admin/edit.php?post_type=product page.

So, here’s a very simple snippet for you. Enjoy!

Once the snippet below is active, WooCommerce products will be sorted by stock status (on load)

PHP Snippet: Sort Products by Stock Status @ WooCommerce Admin

/**
 * @snippet       Product Sorted By Stock @ WordPress Dashboard
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 5
 * @donate $9     https://tutoraspire.com
 */

add_action( 'pre_get_posts', 'tutoraspire_sort_products_by_stock_status_admin' );

function tutoraspire_sort_products_by_stock_status_admin( $query ){
global $typenow;
if ( is_admin() && $query->is_main_query() && $typenow == 'product' ) {
if ( ! isset( $_GET['orderby'] ) ) {
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_key', '_stock_status' );
$query->set( 'order', 'ASC' );
}
}
}

You may also like