white and black train station

Hide out of stock items from products shortcodes

If you’re managing an e-commerce website, it’s crucial to offer a seamless shopping experience for your customers. One way to enhance this experience is by hiding out-of-stock items from product shortcodes. This ensures that customers only see products that are available for purchase, thereby streamlining their browsing and decision-making process. Not only does this reduce potential frustration for the customer, but it also makes inventory management more straightforward for the business. By implementing this feature, you can maintain a cleaner, more efficient product display, which can ultimately contribute to higher conversion rates and improved customer satisfaction.

Add this to you functions.php

add_filter( 'woocommerce_shortcode_products_query', function ( $query_args, $atts, $loop ) {
  if ( str_contains( $atts['class'], 'hide-out-of-stock' ) ) {
    $query_args['meta_query'] = [
        'key'     => '_stock_status',
        'value'   => 'outofstock',
        'compare' => 'NOT LIKE',

  return $query_args;
}, 10, 3 );

This will only be applied if the [products] block has a class set. Ex;

[products limit="4" class="hide-out-of-stock your-other-classes" category="extras" orderby="rand"]


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.