HPOS NOTES
The regexp search is part of the database migration alert because HPOS moves WooCommerce orders from WordPress posts tables to custom order tables. The regexp helps find code that:
- Uses direct database queries (
wpdb
) - Uses WordPress post functions (
get_post
,update_post_meta
, etc.) - References order-specific terms (
shop_order
)
It’s in your screenshot because the database sync tool is identifying tables and data that need migration, particularly WooCommerce orders that need to move from post tables to HPOS tables.
You’ll need to update any custom code using these old methods to use WooCommerce’s CRUD API instead, like:
phpCopy// Old
get_post_meta($order_id, '_billing_email', true);
// New
$order = wc_get_order($order_id);
$order->get_billing_email();
CopyRetry
2 / 2
E
can you elaboraote on e WooCommerce’s CRUD API and HPOS?
CRUD API and HPOS work together in WooCommerce:
CRUD API (Create, Read, Update, Delete):
phpCopy// Create
$order = wc_create_order();
// Read
$order = wc_get_order($id);
$email = $order->get_billing_email();
// Update
$order->set_billing_email('new@email.com');
$order->save();
// Delete
$order->delete();
HPOS (High-Performance Order Storage):
- Moves orders from wp_posts to dedicated tables
- Tables: wp_wc_orders, wp_wc_orders_meta, wp_wc_order_addresses
- Better performance for high-volume stores
- Requires using CRUD API instead of WordPress functions
Example Migration:
phpCopy// Old way (don't use)
$meta = get_post_meta($order_id, '_billing_email', true);
// New way with CRUD
$order = wc_get_order($order_id);
$meta = $order->get_billing_email();
Key CRUD Methods:
phpCopy$order->get_*() // Getters
$order->set_*() // Setters
$order->update_meta_data()
$order->get_meta()
$order->save()
CopyRetry
E
what code should i look out for to update besides this?
Key code patterns to update for HPOS compatibility:
- Direct SQL:
phpCopy$wpdb->get_results("SELECT * FROM wp_posts WHERE post_type = 'shop_order'");
// Change to: wc_get_orders()
- Post Meta Functions:
phpCopyget_post_meta($order_id, '_billing_email');
update_post_meta($order_id, '_payment_method');
// Change to: $order->get_billing_email(), $order->set_payment_method()
- Post Functions:
phpCopyget_post($order_id);
wp_update_post();
// Change to: wc_get_order(), $order->save()
- Order Item Access:
phpCopy$wpdb->prefix . 'woocommerce_order_items'
$wpdb->prefix . 'woocommerce_order_itemmeta'
// Change to: $order->get_items(), $item->get_meta()
- Order Status Changes:
phpCopywp_trash_post($order_id);
// Change to: $order->update_status('trash')
CopyRetry
E
explain the usage of this wp_trash_post($order_id);
wp_trash_post($order_id)
is WordPress’s function to move posts to trash. For WooCommerce orders, use HPOS-compatible methods instead:
phpCopy// Old way
wp_trash_post($order_id);
// HPOS way
$order = wc_get_order($order_id);
$order->update_status('trash');
// Or
$order->delete(true); // true = move to trash, false = permanent delete
Common use cases:
phpCopy// Bulk trash orders
foreach($order_ids as $id) {
$order = wc_get_order($id);
$order->update_status('trash');
}
// Trash with note
$order = wc_get_order($order_id);
$order->add_order_note('Order trashed due to...');
$order->update_status('trash');
CopyRetry