我的商店里有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} ";
}
这是打印产品:30页:5个帖子:1,即没有shop_orders.我想我遗漏了非常明显的东西,但是对我来说,那个明显的东西并不是那么明显!
更新:我现在使用以下代码检索所有订单:
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => -1
);
$posts = get_posts($args);
但是,这并不能回答问题.但这是一个解决方案.
解决方法:
使用’post_status’=> ‘wc-processing’或’post_status’=> ‘任何’