Php WooCommerce:过去30天最畅销产品的定制循环

Php WooCommerce:过去30天最畅销产品的定制循环,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我想展示过去30天最畅销的产品。 我已经有了展示畅销产品的代码。但这些是商店的总销售额。 有没有办法将产品销售限制在30天内 我猜meta键total_sales不是正确的方法,对吧 以下是我当前的循环: $args_basic = array( 'post_type' => 'product', 'posts_per_page' => 12, 'meta_key' => 'total_sales', 'o

我想展示过去30天最畅销的产品。 我已经有了展示畅销产品的代码。但这些是商店的总销售额。 有没有办法将产品销售限制在30天内

我猜meta键
total_sales
不是正确的方法,对吧

以下是我当前的循环:

$args_basic = array(
    'post_type'         => 'product',
    'posts_per_page'    => 12,
    'meta_key'          => 'total_sales',
    'orderby'           => 'meta_value_num',
);

我没有办法测试以下内容,但乍一看,它应该是从壁虎工作

  • 我们通过参数后的
    日期\查询上月订单
  • 然后,我们检索每个订单项ID并将它们推送到一个新的
    数组
  • 最后,我们可以使用来计算每个ID出现的时间

对智者说,不要删除新问题。我正在修改你的合并查询问题的答案,浪费了15分钟。@AmarineDialog我很抱歉!该问题有几个关闭请求:-(我应该再次打开它吗?但我认为它会在几分钟后关闭谢谢!我将测试它:)更新,并将
rsort()
(按降序排序数组)添加到结果转储。
<?php

add_action( 'init', 'wpso_67421248' );
  
function wpso_67421248() {

   $orders = wc_get_orders(
        array(
            'limit' => -1,
            'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ),
            'date_after' => date( 'Y-m-d', strtotime( '-1 month' ) ),
            'return' => 'ids',
        )
   );

   $identifiers = array();

   foreach ( $orders as $order ) {

        $items = wc_get_order( $order )->get_items();

        foreach ( $items as $item ) {

            array_push( $identifiers, $item->get_product_id() );

        };

    };

    var_dump( rsort( array_count_values( $identifiers ) ) );

};