繁体   English   中英

如何使用 wp_query woocommerce 上周和上个月获得最畅销的产品

[英]How to get best selling products using wp_query woocommerce last week and last month

我想按上周和上个月对主页上的畅销产品进行排序。 我正在使用下面的查询但这并没有获得所有类别的畅销产品。

function get_product_posts_hp(){
    $query = new WP_Query( array(
        'posts_per_page' => 12,
        'post_type' => 'product',
        'post_status' => 'publish',
        'ignore_sticky_posts' => 1,
        'meta_key' => 'total_sales',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
    ) );

    echo '<p>Count: '. $query->post_count ; '</p>';

    if($query->have_posts()) :
        while($query->have_posts()) : $query->the_post();

            echo '<p>' . get_the_title() . ' (';
            echo get_post_meta( get_the_id(), 'total_sales', true) . ')</p>';

        endwhile;
        wp_reset_postdata();
    endif;
}

您的查询不是获得此信息的最佳方式,因为您希望有一段时间。 您必须使用帖子类型 shop_orders 获取特定时间段内状态为完整的所有订单,然后将订单中的订单项目分解为项目,您将获得一段时间内最畅销的产品。 或者,如果您不想要一段时间,那么您可以将其添加到您的查询中

    $query = new WP_Query( array(
    'posts_per_page' => 12,
    'post_type' => 'product',
    'post_status' => 'publish',
    'ignore_sticky_posts' => 1,
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'meta_query'=> array(
    array(
      'key' => 'total_sales',
      'compare' => '>=',
      'value' => 1,
      'type' => 'numeric',
    )
  )
) );

您可以根据价值更改此产品至少必须具有的总销售额:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM