📦 Move Out-of-Stock Products to the end of Pagination (WooCommerce Snippet)

Nobody likes browsing a shop only to find half the products are out of stock. It’s frustrating for customers and bad for conversions.

Here’s a simple code snippet that pushes out-of-stock products to the end of the product listings—on shop, category, and tag archive pages.

đź§© The Code

// Move out-of-stock items to the end
function modify_product_query_for_stock_status($query) {
    if (!is_admin() && $query->is_main_query() && (is_shop() || is_product_category() || is_product_tag())) {
        add_filter('posts_join', 'add_stock_status_join');
        add_filter('posts_orderby', 'add_stock_status_orderby');
    }
}
add_action('pre_get_posts', 'modify_product_query_for_stock_status');

function add_stock_status_join($join) {
    global $wpdb;
    $join .= " LEFT JOIN {$wpdb->postmeta} AS stock_meta ON ({$wpdb->posts}.ID = stock_meta.post_id AND stock_meta.meta_key = '_stock_status')";
    remove_filter('posts_join', 'add_stock_status_join');
    return $join;
}

function add_stock_status_orderby($orderby) {
    global $wpdb;
    $orderby = "stock_meta.meta_value ASC, $wpdb->posts.post_date DESC";
    remove_filter('posts_orderby', 'add_stock_status_orderby');
    return $orderby;
}

📍 Where to Add It

Place this snippet in your theme’s functions.php file or add it using a code snippet manager plugin like Code Snippets.

🔍 What It Does

  • Prioritizes products with _stock_status = instock
  • Pushes outofstock products to the bottom of the list
  • Works on shop, category, and tag pages
  • Doesn’t affect the admin or unrelated queries

âś… Why It Matters

  • Improves customer experience
  • Highlights in-stock products first
  • Avoids confusion or disappointment when browsing

🛠️ Want More UX Improvements?

At EUX Digital Agency, we help WooCommerce stores improve performance, conversion, and customer experience. Whether it’s smart tweaks like this or custom development—we’ve got you covered.

đź“© Reach out here if you need support or customizations!

Call Talk to us
4.9(35)