Update WooCommerce Order Content
August 7, 2025 ยท AI Conversations
UPDATE wp_posts p
JOIN wp_postmeta pm ON pm.post_id = p.ID
SET p.post_content = pm.meta_value
WHERE p.post_type = 'shop_order'
AND pm.meta_key = '_your_meta_key';
### Replace:
– `_your_meta_key` with the actual meta key you want to use as the source of the content.
### Example:
If you want to update `post_content` with the value of `_customer_note`, the query becomes:
UPDATE wp_posts p
JOIN wp_postmeta pm ON pm.post_id = p.ID
SET p.post_content = pm.meta_value
WHERE p.post_type = 'shop_order'
AND pm.meta_key = '_customer_note';
> โ ๏ธ **Backup your database before running any direct SQL update**, especially on production environments. This operation cannot be undone.
Let me know if you want to apply conditions like updating only empty `post_content`, or use a different `meta_key`.
—
### โ Example: Append `_customer_note`, `_billing_email`, and `_billing_phone` into `post_content`
UPDATE wp_posts p
JOIN wp_postmeta note ON note.post_id = p.ID AND note.meta_key = '_customer_note'
JOIN wp_postmeta email ON email.post_id = p.ID AND email.meta_key = '_billing_email'
JOIN wp_postmeta phone ON phone.post_id = p.ID AND phone.meta_key = '_billing_phone'
SET p.post_content = CONCAT(
'Note: ', note.meta_value, 'n',
'Email: ', email.meta_value, 'n',
'Phone: ', phone.meta_value
)
WHERE p.post_type = 'shop_order';
—
### ๐ Optional Variation (if you want to *append* to existing `post_content` instead of replacing):
SET p.post_content = CONCAT(
p.post_content, 'n---n',
'Note: ', note.meta_value, 'n',
'Email: ', email.meta_value, 'n',
'Phone: ', phone.meta_value
)
—
### โ
Notes:
– Adjust meta keys like `_customer_note`, `_billing_email`, and `_billing_phone` as per your needs.
– Make sure all `post_id` have those meta keys, otherwise the `JOIN` will skip that row. Use `LEFT JOIN` if some may be missing.
– Always backup before running updates.
Would you like a version with `LEFT JOIN` fallback handling if some meta keys may be missing?
—
### โ SQL to Append One Meta Value to `post_content`:
UPDATE wp_posts p
JOIN wp_postmeta pm ON pm.post_id = p.ID AND pm.meta_key = '_your_meta_key'
SET p.post_content = CONCAT(p.post_content, 'n', pm.meta_value)
WHERE p.post_type = 'shop_order';
—
### ๐ Replace `_your_meta_key` with the actual meta key
For example, to append `_customer_note`:
UPDATE wp_posts p
JOIN wp_postmeta pm ON pm.post_id = p.ID AND pm.meta_key = '_customer_note'
SET p.post_content = CONCAT(p.post_content, 'n', pm.meta_value)
WHERE p.post_type = 'shop_order';
—
### ๐ Reminder:
– Backup your database first.
– `CONCAT` handles `NULL` as NULL โ so ensure `post_content` is not `NULL` (or use `IFNULL(p.post_content, '')`).
Let me know if you'd like to include a label like "Customer Note: …" before the appended value.
—
### โ SQL to Append One Meta Value with a Comma:
UPDATE wp_posts p
JOIN wp_postmeta pm ON pm.post_id = p.ID AND pm.meta_key = '_your_meta_key'
SET p.post_content = CONCAT(
IFNULL(p.post_content, ''),
CASE
WHEN p.post_content IS NULL OR p.post_content = '' THEN ''
ELSE ', '
END,
pm.meta_value
)
WHERE p.post_type = 'shop_order';
—
### ๐ Example with `_customer_note`:
UPDATE wp_posts p
JOIN wp_postmeta pm ON pm.post_id = p.ID AND pm.meta_key = '_customer_note'
SET p.post_content = CONCAT(
IFNULL(p.post_content, ''),
CASE
WHEN p.post_content IS NULL OR p.post_content = '' THEN ''
ELSE ', '
END,
pm.meta_value
)
WHERE p.post_type = 'shop_order';
—
### โ
What This Does:
– Appends the meta value after a comma **only if** `post_content` is not empty.
– Avoids starting with a comma when `post_content` is initially empty.
Let me know if you want to also trim or limit content size or add multiple meta keys in the same format.