Menu

Tarei King

WordPress developer, lover and enthusiast. Based in New Zealand, working Globally.

Build a custom WP Query loop for getting WooCommerce ‘out of stock’ products

Creating a template to show only sold / out of stock items is sometimes neccesary when clients sell one-off items. I took the approach of creating a custom page template which allowed me to turn off “Show out of stock items” in all other WooCommerce pages. This allows visibility of these items on the specified page only.

I made a quick page template which would only get these products by using WordPress Meta Query properties in the loop and you can combine the meta key/values with the information found in a comment by Tareq on Meta Keys for attributes.

Here is the loop I created for this run:

$args = array( 'post_type' => 'product', 'posts_per_page' => 30, 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => '='
)
)
);
$loop = new WP_Query( $args );

If you want to get items which are in stock only, you can change the value to “instock”.

Leave a Reply