function custom_hide_products_with_zero_stock($q) {
    //if (is_admin()) return $q; // Don't filter in the admin area

    $meta_query = $q->get('meta_query');

    // Add a condition to exclude products with stock quantity 0
    $meta_query[] = array(
        'key' => '_stock',
        'value' => 1, // Adjust this value as needed (1 for products with stock > 0)
        'compare' => '>',
        'type' => 'NUMERIC'
    );

    $q->set('meta_query', $meta_query);

    return $q;
}
add_filter('woocommerce_product_query', 'custom_hide_products_with_zero_stock');