I have some 30 orders in my shop. I am trying to loop over all orders, but I can't retrieve any orders. Here's the code:
$args = array (
'post_type' => 'shop_order',
'posts_per_page' => - 1
);
$loop = new WP_Query($args);
while ($loop->have_posts()) {
// do some work here
}
The loop never runs. I tried printing a count of all post types:
$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} ";
}
This is printing product: 30 page: 5 post: 1
, i.e. no shop_order
s. I guess I'm missing something very obvious, but it's not so obvious to me what that obvious thing is!
UPDATE: I am now retrieving all orders with this code:
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => -1
);
$posts = get_posts($args);
That doesn't answer the question though. But it's a solution.
use 'post_status' => 'wc-processing'
or 'post_status' => 'any'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With