ylliX - Online Advertising Network

How to find Woocommerce products out of stock with stock greater then zero?

Sometimes, especially if you are using custom plugins to import stock data, you may find products with some stock value (greater then 0) but with stock flag set to “out of stock”. How to find them? Using WP_Query of course:

$args = array(
	'post_type' => 'product_variation',
	'posts_per_page' => -1,
	'meta_query' => array(
		array(
			'key' => '_stock_status',
			'value' => 'outofstock',
		),
		array(
			'key' => '_stock',
			'value' => '0',
			'compare' => '>',
			'type' => 'NUMERIC'
		),
	),
);
$products = new WP_Query($args);

Notice ‘post_type’ => ‘product_variation’ – if you are not using variations, just change it to ‘product’.

Leave a Reply