Woocommerce product query for stock status
It's highly recommended to use WC_Product_Query()
instead of WP_Query()
.
So, i am showing how you can do this using WC_Product_Query()
.
$query_args = array(
'limit' => 10, //or whatever number of products you want to get.
'stock_status' => 'instock' // or 'outofstock' or 'onbackorder'
);
$query = new WC_Product_Query( $query_args );
$products = $query->get_products();
Note: If you want to include multiple stock_status, you can just simply use an array.
'stock_status' => array('instock', 'outofstock', 'onbackorder')
You can have multiple meta_query filters. See (http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters). By default the relationship between these filters is AND which is ok. You can add a filter for _back_order = no
.
$query = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock'
),
array(
'key' => '_backorders',
'value' => 'no'
),
)
);
$wp_query = & new WP_Query($query);
while ($wp_query->have_posts()) : $wp_query->the_post();