我的店里有大约 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);
但这并没有回答问题。但这是一个解决方案。
最佳答案
使用 'post_status' => 'wc-processing'
或 'post_status' => 'any'
关于php - 为什么我有 WooCommerce 订单,但没有 shop_order 类型的帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43106393/