Woocommerce wp_query get order by ID Woocommerce wp_query get order by ID wordpress wordpress

Woocommerce wp_query get order by ID


Update 2

To get the order data for one order, you don't need WP_query. You can use directly:

$order = wc_get_order( $order_id );$order->id; // order ID$order->post_title; // order Title$order->post_status; // order Status// getting order itemsforeach($order->get_items() as $item_id => $item_values){    // Getting the product ID    $product_id = $item_values['product_id'];    // .../...}

Update 1

You should try this, as with array_keys( wc_get_order_statuses() you will get all order statuses and with 'numberposts' => -1, all existing orders.

Here is an alternative way (without WP_query or you can use thoses args in the WP_query array):

$customer_orders = get_posts( array(     'numberposts'    => -1,    'post_type' => 'shop_order',    'post_status'    => array_keys( wc_get_order_statuses() ) ) );// Going through each current customer ordersforeach ( $customer_orders as $customer_order ) {    // Getting Order ID, title and status    $order_id = $customer_order->ID;    $order_title = $customer_order->post_title;    $order_status = $customer_order->post_status;    // Displaying Order ID, title and status    echo '<p>Order ID : ' . $order_id . '<br>';    echo 'Order title: ' . $order_title . '<br>';    echo 'Order status: ' . $order_status . '<br>';    // Getting an instance of the order object    $order = wc_get_order( $order_id );    // Going through each current customer order items    foreach($order->get_items() as $item_id => $item_values){        // Getting the product ID        $product_id = $item_values['product_id'];        // displaying the product ID        echo '<p>Product ID: '.$product_id.'</p>';    }}


The strict answer to this question is change 'p'=>$post_id' to 'post__in' => array($post_id)

...but the real answer, should anyone be treading this path, is that parsing the email is so much easier, woocommerce has done most of the work for you. There are other pratfalls along the way; 1. The post is protected and the order notes are in the excerpt so can't be displayed ( I could move them to meta but...) 2. The order items are within comments and have to be looped then parsed to produce meaningful output, so I might as well parse the email direct.