WooCommerce: Sort Products By Stock @ Admin Dashboard

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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 5
 * @community     https://businessbloomer.com/club/
 */

add_action( 'pre_get_posts', 'bbloomer_sort_products_by_stock_status_admin' );

function bbloomer_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' );
		}
	}
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

2 thoughts on “WooCommerce: Sort Products By Stock @ Admin Dashboard

  1. Hello,
    The code works but it does not short the products correctly from Product with most stock to product with least stock.
    As of now it shows product in stock first organized randomly (no matter the quantity in stock) and then out of stock items.
    How could I get the product in stock organized by stock quantity most quantity to least quantity?
    Many thanks
    Federico

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *