Get products in stock range with wc_get_products()

[ad_1]

Hello @katmacau,

As you mentioned, wc_get_products() only works for a single quantity, but not a range. To get the IDs of all products that have a stock between 1 and 30, you could use the following query:

$args = array(
  'post_type'   => 'product',
  'post_status' => 'publish',
  'meta_query'  => array(
    array(
      'key'     => '_stock',
      'value'   => array( 1, 30 ),
      'compare' => 'BETWEEN',
    ),
  ),
  'fields' => 'ids'
);
$products = new WP_Query( $args );
$product_ids = $products->posts;

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer