title: Filter products by price range using WooCommerce wc_get_products
body: <p>I'm getting a bunch of paginated variable products using <a href="https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query" rel="nofollow noreferrer">wc_get_products()</a>. And I would like to only get products in a given price-range. The problem is that there are a lot of products, so only 20 products are fetched at a time.</p>
  <p>According to the documentation, there are no <code>meta_query()</code>-option when using <code>wc_get_products()</code>.</p>
  <p>So how do I get this?</p>
  <p>Here is my current query:</p>
  <pre><code>$products_query = [
      'category' =&gt; [ get_queried_object()-&gt;slug ],
      'limit' =&gt; 20,
      'paginate' =&gt; true,
      'paged' =&gt; (get_query_var('paged')) ? get_query_var('paged') : 1,
  ];
  
  $products = wc_get_products( $products_query );
  </code></pre>
  <p>Now remember, that it's something that needs to be implemented before the products are queried from the database.
  Let's say that I have 100 products total. Since the products are paginated, then if I have ordered the products after <code>_price</code> (ASC), then page 1 should return the 20 cheapest products. And page 3 should return the products 40-60 (after they have been sorted).</p>
  <hr />
  <h2>Solution attempts</h2>
  <p><strong>Attempt 1 - Try using a meta_query anyways</strong></p>
  <p>I figured that wc_get_products was inspired and built on top of WP_Query, so I tried this:</p>
  <pre><code>$products_query = [
      'category' =&gt; [ get_queried_object()-&gt;slug ],
      'limit' =&gt; 20,
      'paginate' =&gt; true,
      'paged' =&gt; (get_query_var('paged')) ? get_query_var('paged') : 1,
      'meta_query' =&gt; [
          'relation' =&gt; 'AND',
          [
              'key' =&gt; '_price',
              'value' =&gt; 100,
              'compare' =&gt; '&lt;=',
              'type' =&gt; 'NUMERIC'
          ]
      ]
  ];
  
  $products = wc_get_products( $products_query );
  </code></pre>
  <p>It just ignores the <code>meta_query</code>-part and returns a result as if it wasn't there.</p>
  <hr />
  <p><strong>Attempt 2 - Using WP_Query</strong></p>
  <p>Everywhere I look, there are arrows pointing towards <code>wc_get_products</code> and away from <code>wp_query</code>.
  So I haven't pursued this.</p>
  <hr />
  <p><strong>Attempt 3 - Raw SQL</strong></p>
  <p>One solution would be to make a raw SQL-query. But since the products are variable products, then the SQL-query would be quite a few joins, since it should first find all products - and then find all variations. And then sort the products after the lowest variation-price (if the price-sort is ascending) - and after the highest variation-price (if the price-sort is descending). It's obviously possible, but I was hoping to find a more WordPress-like solution. And I'm not good at SQL.</p>
  <hr />
  <p><strong>Attempt 4 - Using a plugin</strong></p>
  <p>I look around a bit - and sort'n'filter-plugins for WooCommerce are <em>monsters</em>, since they slab scripts and styles left, right and center. So this seems like a poor solution.</p>
  <hr />
  <p><strong>Attempt 5 - Using WooCommerce API</strong></p>
  <p>I can see that <code>min_price</code> and <code>max_price</code> are mentioned under <a href="https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products" rel="nofollow noreferrer">List all products</a>. So this might actually work.</p>