1 回答
TA贡献1906条经验 获得超10个赞
您需要另一次 使用自己的参考JOIN表,因为您正在该表上查询 2 次不同的元键,因此请尝试以下操作:wp_woocommerce_order_itemmeta
function get_last_order_id_from_product( $product_id ) {
global $wpdb;
return $wpdb->get_col( $wpdb->prepare( "
SELECT oi.order_id
FROM {$wpdb->prefix}woocommerce_order_items as oi
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as oim
ON oi.order_item_id = oim.order_item_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as oim2
ON oi.order_item_id = oim2.order_item_id
LEFT JOIN {$wpdb->posts} AS p
ON oi.order_id = p.ID
WHERE p.post_type = 'shop_order'
AND oi.order_item_type = 'line_item'
AND oim.meta_key = '_product_id'
AND oim.meta_value = '%d'
AND oim2.meta_key = 'Ticket Number'
ORDER BY oi.order_id DESC
LIMIT 1, 1
", $product_id ) );
}
测试和工作。
- 1 回答
- 0 关注
- 206 浏览
添加回答
举报