Bhanu
Bhanu

Reputation: 351

Get order ID of logged in user using WP_Query

I want to get all the IDs of Orders made by the logged in customer.

This is how I have got the orders:

// Fetch User   
 $user = wp_get_current_user();
 // Get User ID
 $user_id = $user->id;
 //Query to get all the users
 $customer_orders = get_posts( array(
                    'meta_key'    => '_customer_user',
                    'meta_value'  => $user_id,
                    'post_type'   => 'shop_order',
                    'post_status' => array_keys( wc_get_order_statuses() ),
                    'numberposts' => -1
                ));

var_dump( $customer_orders);

The Dump is :

array(1) { [0]=> object(WP_Post)#7753 (24) { ["ID"]=> int(1847) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2019-08-07 07:47:29" ["post_date_gmt"]=> string(19) "2019-08-07 07:47:29" ["post_content"]=> string(0) "" ["post_title"]=> string(39) "Order – August 7, 2019 @ 07:47 AM" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(13) "wc-processing" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(22) "wc_order_Iy3n87YMr5Y68" ["post_name"]=> string(25) "order-aug-07-2019-0747-am" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2019-08-07 07:47:29" ["post_modified_gmt"]=> string(19) "2019-08-07 07:47:29" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(73) "https://www.schools2u.krenovate.xyz/shop_order/order-aug-07-2019-0747-am/" ["menu_order"]=> int(0) ["post_type"]=> string(10) "shop_order" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "1" ["filter"]=> string(3) "raw" } }

I can see the ID I want from the dump

 ["ID"]=> int(1847) 

I am trying to get fetch it like this :

foreach($customer_orders as $order_id){
 $post_id = get_the_ID($order_id);
 var_dump($post_id);    
}

It is not throwing back the desired ID.

Upvotes: 0

Views: 149

Answers (1)

gjzim
gjzim

Reputation: 633

Try this:

foreach($customer_orders as $order){
 $post_id = $order->ID;
 var_dump($post_id);    
}

Upvotes: 1

Related Questions