title: Improve WC_Product_Query on Woocommerce products with variations
body: <p>I've written a function to loop through all products, finding variations that are out of stock and then deleting the variation and the variable products associated attributes so that any product filter plugins do not show specific variations when they are out of stock.</p>
  
  <p>My function works fine on my test site but I'm looking for some advice on making this more efficient as I have some concerns about running it on a live site which has hundreds of products with around 15 variations per product.</p>
  
  <p>Here's my function code:</p>
  
  <pre><code>function wpmad_purge_variations_not_in_stock(){
  
      echo 'Purging products...&lt;br&gt;&lt;br&gt;';
  
      $args = array( 'status' =&gt; 'publish', 'limit' =&gt; -1 );
      $products = wc_get_products( $args );
  
      foreach ( $products as $product ){
  
          if ( $product-&gt;is_type( 'variable' ) ){
  
              $product_id = $product-&gt;id;
              // Get all available product variations for current product/item
              $variations = $product-&gt;get_available_variations();
  
              // Loop through each of the available variations
              foreach ( $variations as $variation ){
  
                  // Check if variation is no longer in stock
                  if ( $variation['is_in_stock'] == '' ){
  
                      $variation_id = $variation['variation_id'];
  
                      // Attribute counter
                      $count = 1;
  
                      // For each variation attribute
                      foreach ( $variation['attributes'] as $att_name =&gt; $att_value ){
  
                          $atts[$count]['name'] = str_replace( 'attribute_', '', $att_name );
                          $atts[$count]['value'] = $att_value;
  
                          $count++; // Increase counter for each loop
  
                      }
  
                      // Delete product variation post
                      echo 'Deleted product variation ID #' . $variation_id . '&lt;br&gt;';
                      wp_delete_post( $variation_id, true );
  
                      // For each attribute
                      foreach ( $atts as $att ){
                          // Remove attribute from main product
                          echo 'Deleted product attribute (' . $att['name'] . ' - ' . $att['value'] . ') for product ID #' . $product_id . '&lt;br&gt;&lt;br&gt;';
                          wp_remove_object_terms( $product_id, $att['value'], $att['name'] );
                      }
  
                  }
  
              }
  
          }
      }
  
      echo 'Product variations have now been purged if out of stock';
  
  }
  </code></pre>
  
  <p>Is it possible to use the function <code>wc_get_products()</code> to only return variable products and are there any efficiency/peformance improvements that can be made to my code?</p>
  
  <p>Thanks in advance!</p>