Php WooCommerce 3+上管理后端的未决订单问题摘要;

Php WooCommerce 3+上管理后端的未决订单问题摘要;,php,wordpress,woocommerce,posts,orders,Php,Wordpress,Woocommerce,Posts,Orders,将Woocommerce从2.6.13更新到3.2.6后,一些显示待定订单和汇总订购产品的自定义代码不再有效。我在移植代码时遇到了问题,因为我一直查找的woocommerce文档似乎已经过时 例如,我认为这不再有效,但我找不到更新版本 $orders = get_posts( array( 'post_type' => 'shop_order', 'post_status' => array( 'wc-processing', 'wc-completed' ) )

将Woocommerce从2.6.13更新到3.2.6后,一些显示待定订单和汇总订购产品的自定义代码不再有效。我在移植代码时遇到了问题,因为我一直查找的woocommerce文档似乎已经过时

例如,我认为这不再有效,但我找不到更新版本

$orders = get_posts( array(
    'post_type'   => 'shop_order',
    'post_status' => array( 'wc-processing', 'wc-completed' )
) );
我已经从下面的一个更新了它,但都没有返回数组中的任何内容

    $orders = get_posts( array(
    'post_type'   => 'shop_order',
    'post_status' => 'publish',
    'tax_query'   => array( array(
            'taxonomy' => 'shop_order_status',
            'field'           => 'slug',
            'terms'         => array( 'processing', 'completed' )
    ) )
) );

使用3.0+中的woocommerce中的get_posts获取订单的正确方法是什么?

您的第一个代码片段在WC 3+中有效,可以获取
WP_Post
订单对象数组,但您需要以这种方式指定发布数量:

$post_orders = get_posts( array(
    'post_type'   => 'shop_order',
    'numberposts' => -1, // number of post (all)
    'post_status' => array( 'wc-processing', 'wc-completed' )
) );

// Display the number of Post orders objects in the array
echo count($post_orders);
或者,您可以使用以下SQL查询:

global $wpdb;

$post_orders = $wpdb->get_results( "
    SELECT *
    FROM {$wpdb->prefix}posts
    WHERE post_type LIKE 'shop_order'
    AND post_status IN ('wc-processing', 'wc-completed')
" );

// Display the number of WP_Post orders in the array
echo count($post_orders);
要获取
WC\u Order
对象的数组,可以使用:

$orders = wc_get_orders( array(
    'numberposts' => -1, // number of post (all)
    'post_status' => array( 'wc-processing', 'wc-completed' )
) );
// Display the number of WP_Order orders in the array
echo count($orders);

谢谢我现在使用你的两种方法检索11份订单。但是,代码仍然没有显示实际数据。一个问题:如果我省略了-1,那么我在计数中只显示了5个订单,而不是11个订单。有什么区别?@ComputerGiant该代码适用于自WC 2.6.x以来的所有版本…我认为问题出在您的数据库中…当woocommerce从2.6.x更新时,数据库也出现了问题…可能是其他原因,因为我认为您也更新了所有插件和Wordpress。因此,您应该检查wp_post表…我的答案只是回答了你最初的问题;我可以看到订单数据。我只需要看看其他程序员是如何把它吐出来的,以找出他哪里出了错,但我仍然对使用-1表示numberpost时得到的值的差异感兴趣,并且完全忽略它。使用number of post=-1,在我的测试服务器上我得到78个订单,没有它我只得到5个…谢谢。也许这是一种组块形式,Wordpress/WC默认为5个结果,除非指定。谢谢你的帮助!