我想在特定日期之前退回WooCommerce中按其受欢迎程度(总销售额)排序的所有产品。
$args = array(
'post_type' => 'products',
'posts_per_page' => 2000,
'orderby' => 'meta_value_num',
'meta_key' => 'total_sales',
'order' => 'desc',
'date_query' => array(
'column' => 'order_date',
array(
'after' => 'April 1st 2017',
),
),
);
$products = new WP_Query( $args );
if ( $products->have_posts() ) {
while ( $products->have_posts() ) : $products->the_post();
the_title();
echo '<br>';
endwhile;
}
此查询返回自April 1st 2017
以来添加的按受欢迎程度排序的所有商品,而不是自April 1st 2017
以来的总产品销售额。
答案 0 :(得分:0)
您的post_type
看起来像是错误的。
您要查询product
而不是shop_order
:
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => 2000,
'orderby' => 'meta_value_num',
'meta_key' => 'total_sales',
'order' => 'desc',
'date_query' => array(
'column' => 'order_date',
array(
'after' => 'April 1st 2017',
),
),
);
答案 1 :(得分:0)
您可以使用此查询在特定日期之间获得最畅销的产品:
$query_str = "SELECT order_item_meta_2.meta_value as product_id, SUM( order_item_meta.meta_value ) as line_total
FROM wpfi_woocommerce_order_items as order_items
LEFT JOIN wpfi_woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN wpfi_woocommerce_order_itemmeta as order_item_meta_2 ON order_items.order_item_id = order_item_meta_2.order_item_id
LEFT JOIN wpfi_posts AS posts ON order_items.order_id = posts.ID
WHERE posts.post_type = 'shop_order'
AND posts.post_date BETWEEN '".$previous_date."' AND '".$current_date."'
AND posts.post_status IN ( 'wc-completed')
AND order_items.order_item_type = 'line_item'
AND order_item_meta.meta_key = '_line_total'
AND order_item_meta_2.meta_key = '_product_id'
GROUP BY order_item_meta_2.meta_value LIMIT 5";
答案 2 :(得分:-1)
我认为你必须使用post_type = order。
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => 2000,
'orderby' => 'meta_value_num',
'meta_key' => 'order_total',
'order' => 'desc',
'date_query' => array(
'column' => 'post_date',
array(
'after' => 'April 1st 2017',
),
),
);