Query in-stock products WooCommerce WordPress.

The online shop we were building had a lot of products those out of stock. We really wanted not to show them in our online shop. Those products are not only out of stock but also had some other issues. The WordPress WP_Query is very handy to filter those.

Following is the code segment to skip the out of stock products using WordPress:

$args = array(
    'post_type'	=> 'product',
    'post_status'	=> 'publish',
    'ignore_sticky_posts' => 1,
    'posts_per_page' => 10,
    'meta_query'     => array(
        'key'           => '_stock_status',
        'value'         => 'instock',
        'compare'       => '='
    )
);
$products = new WP_Query( $args );