Home » WooCommerce: Display “In Stock” Products First @ Shop

WooCommerce: Display “In Stock” Products First @ Shop

by adminkys

We’ve already seen how to add a custom “Product Sorting” option to the “Default Sorting” dropdown in the WooCommerce Shop page.

The task I was presented with, however, was to display items based on a custom “meta key”. Now, if you have no idea what a “meta key” is, don’t worry too much. For example, “_stock_status” is one of these keys, and therefore you can sort products by that key as opposed to product name, date, price, etc.

So, let’s see how to show all out of stock products as last in the shop, category and loop pages. As usual, simply copy/paste this little plugin in your functions.php and you’ll get the wanted result 🙂

WooCommerce: sort products by stock (push out of stock products to the end of every WooCommerce loop)

PHP Snippet: Sort Products by Stock Status @ WooCommerce Shop

Note 1: this snippet only works when you use “Default Sorting” from the WordPress > Customize > WooCommerce > Product Catalog > Default product sorting section. It won’t override sorting by price / popularity / etc. when these are set as default.

Note 2: this snippet sorts products by “stock_status” in “ASC” (ascending) order. Possible values are “instock” “outofstock” “onbackorder” so it will automatically display them alphabetically like this: 1.instock 2.onbackorder 3.outofstock

Note 3: all your products must be using the “Managing Stock” option in the product edit page > Product Data > Inventory. Otherwise I’m not sure what happens!

 * @snippet       Sort Products By Stock Status - WooCommerce Shop
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 5
 * @donate $9     https://www.tutoraspire.com

add_filter( 'woocommerce_get_catalog_ordering_args', 'tutoraspire_first_sort_by_stock_amount', 9999 );

function tutoraspire_first_sort_by_stock_amount( $args ) {
$args['orderby'] = 'meta_value';
$args['meta_key'] = '_stock_status';
return $args;

You may also like