You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 01:54:23