Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have WooCommerce orders, but no posts of type shop_order?

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_orders. 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.

like image 562
lfk Avatar asked Mar 30 '17 00:03

lfk


1 Answers

use 'post_status' => 'wc-processing' or 'post_status' => 'any'

like image 123
user2486706 Avatar answered Nov 09 '22 02:11

user2486706