title: Sort products leaving out of stock at the end
body: <p>There are many answers in StackOverflow, but old ones, none has worked with the lastest version of Woocommerce for me.</p>
  
  <p>My page is already sorting by recent products, but I like to move all sold out products to the last pages.</p>
  
  <p>Last codes I've tried:</p>
  
  <p>1:</p>
  
  <pre><code>add_action( 'pre_get_posts', function ( $q ) {
  if (   $q-&gt;is_main_query()        // Only target the main query
       &amp;&amp; $q-&gt;is_post_type_archive() // Change to suite your needs
  ) {
      $q-&gt;set( 'meta_key', '_stock_status' );
      $q-&gt;set( 'orderby',  'meta_value'    );
      $q-&gt;set( 'order',    'ASC'           );
  }
  }, PHP_INT_MAX );
  </code></pre>
  
  <p>2:</p>
  
  <pre><code>add_filter( 'woocommerce_shortcode_products_query', function( $query_args, $atts, $loop_name ){
      if( $loop_name == 'recent_products' ){
          $query_args['meta_query'] = array( array(
              'key'     =&gt; '_stock_status',
              'value'   =&gt; 'outofstock',
              'compare' =&gt; 'NOT LIKE',
          ) );
      }
      return $query_args;
  }, 10, 3);
  </code></pre>