为什么我有WooCommerce订单,但没有shop_order类型的帖子?

时间:2017-03-30 00:21:28

标签: php wordpress woocommerce

我店里有30个订单。我试图循环所有订单,但我无法检索任何订单。这是代码:

$args = array (
            'post_type'         => 'shop_order',
            'posts_per_page'    => - 1
);

$loop = new WP_Query($args);

while ($loop->have_posts()) {
    // do some work here
}

循环永远不会运行。我尝试打印所有帖子类型的计数:

$args = array (
        'post_type'         => 'any',
        'posts_per_page'    => - 1
);

$loop = new WP_Query($args);    
$types = array();

while ($loop->have_posts()) {
    $loop->the_post();
    $post_id = get_the_ID();
    $type = get_post_type($post_id);
    if ($types[$type]) $types[$type]++;
    else $types[$type] = 1; 
}

foreach ($types as $type => $count) {
    echo "{$type}: {$count} ";
}

这是打印product: 30 page: 5 post: 1,即没有shop_order。我想我错过了一些非常明显的东西,但对我来说这不是那么明显的东西!

更新:我现在正在使用以下代码检索所有订单:

$args = array(
        'post_type'         => 'shop_order',
        'posts_per_page'    => -1
);

$posts = get_posts($args);

但这并没有回答这个问题。但这是一个解决方案。

1 个答案:

答案 0 :(得分:3)

使用'post_status' => 'wc-processing''post_status' => 'any'