WooCommerce HPOS适配:wc_get_orders如何指定查询特定订单ID?
在WooCommerce HPOS中用wc_get_orders指定查询特定订单ID
问题背景
原代码使用get_posts通过post__in指定要查询的订单ID数组,现在适配WooCommerce HPOS需要改用wc_get_orders,但找不到对应post__in的参数,尝试post__in和include无效。
原get_posts代码:
$orders = get_posts( array( 'post__in' => $order_ids, 'numberposts' => -1, 'order' => 'ASC', 'fields' => 'ids', 'post_type' => array( 'shop_order' ), 'post_status' => $statuses, 'meta_query' => array( array( 'key' => '_my_meta_key', 'value' => '', 'compare' => '!=', ), ), ) );
当前待修改的wc_get_orders代码:
$orders = wc_get_orders( array( '???????' => $order_ids, 'limit' => -1, 'order' => 'ASC', 'return' => 'ids', 'type' => array( 'shop_order' ), 'status' => $statuses, 'meta_query' => array( array( 'key' => '_my_meta_key', 'value' => '', 'compare' => '!=', ), ), ) );
解决方案
在wc_get_orders中,对应get_posts里post__in的参数是**include**,直接将参数名替换为include即可生效:
include参数接收订单ID数组,用法和post__in完全一致- 即使启用了HPOS(High-Performance Order Storage),该参数依然有效,WooCommerce会自动适配新的存储结构
修改后的完整代码:
$orders = wc_get_orders( array( 'include' => $order_ids, 'limit' => -1, 'order' => 'ASC', 'return' => 'ids', 'type' => array( 'shop_order' ), 'status' => $statuses, 'meta_query' => array( array( 'key' => '_my_meta_key', 'value' => '', 'compare' => '!=', ), ), ) );
额外排查点
如果之前尝试include无效,可检查以下情况:
$order_ids数组为空或包含无效订单ID- 订单状态
$statuses与目标订单实际状态不匹配,导致被过滤 - 启用HPOS后,WooCommerce版本需在7.0及以上(HPOS正式引入版本)
内容的提问来源于stack exchange,提问作者PthPndr
相关产品推荐
相关产品推荐

