2017-01-16 2 views
0

У меня есть следующий код:PHP: Получить первый массив в цикле

$customer_orders = get_posts(array(
    'numberposts' => -1, 
    'meta_key' => '_customer_user', 
    'meta_value' => get_current_user_id(), 
    'post_type' => 'shop_order', 
    'post_status' => array_keys(wc_get_order_statuses()), 
    ) 
); 

$last_post_date; 
$loop = new WP_Query($customer_orders); 
foreach ($customer_orders as $orderItem) 
{ 
    $order = wc_get_order($orderItem->ID); 
    $last_post_date = $orderItem->post_date; 
} 

echo $last_post_date; 

Я хочу напечатать $last_post_date, который создается из заказа, сделанного клиентом.

Так что если клиент делает 2-й заказ, я получу массив [0] и [1].

Но на данный момент $last_post_date не печатает post_date от array[0].

Всегда печать даты пост от того, что было сделано первый, не последний,

Спасибо за помощь!

+1

'Но на данный момент $ last_post_date не печатает дату публикации из массива [0]. Он всегда печатает дату публикации из заказа, который был сделан первым, а не последним, 'Что это сейчас? не имеет смысла – Cashbee

ответ

1

Как у вас есть 2-го порядка, поэтому $last_post_date становится заменен заместителем post_date. Поэтому, если вы хотите извлечь только дату первого заказа, вы можете добавить проверку.

Попробуйте этот код:

foreach ($customer_orders as $key => $orderItem) //<-- added $key 
{ 
    if ($key == 0) //only for first element. 
    { 
     $last_post_date = $orderItem->post_date; 
    } 
    $order = wc_get_order($orderItem->ID); 
} 

echo $last_post_date; 

Надеется, что это помогает!

+0

Помог мне много! Thnx! – Justin

0
$customer_orders = get_posts(array(
'numberposts' => -1, 
'meta_key' => '_customer_user', 
'meta_value' => get_current_user_id(), 
'post_type' => wc_get_order_types(), 
'post_status' => array_keys(wc_get_order_statuses()), 
)); 
print_r($customer_orders); 

Попробуйте этот код, как показано ниже, я думаю, это поможет вам.

Array 
(
    [0] => WP_Post Object 
     (
      [ID] => 38 
      [post_author] => 1 
      [post_date] => 2017-01-16 10:00:59 
      [post_date_gmt] => 2017-01-16 10:00:59 
      [post_content] => 
      [post_title] => Order – January 16, 2017 @ 10:00 AM 
      [post_excerpt] => 
      [post_status] => wc-completed 
      [comment_status] => open 
      [ping_status] => closed 
      [post_password] => order_587c99db9ac46 
      [post_name] => order-jan-16-2017-1000-am 
      [to_ping] => 
      [pinged] => 
      [post_modified] => 2017-01-16 10:03:19 
      [post_modified_gmt] => 2017-01-16 10:03:19 
      [post_content_filtered] => 
      [post_parent] => 0 
      [guid] => http://localhost/wordpress/?post_type=shop_order&p=38 
      [menu_order] => 0 
      [post_type] => shop_order 
      [post_mime_type] => 
      [comment_count] => 2 
      [filter] => raw 
     ) 

) 
Смежные вопросы