How can I list best selling products in woocommerce

后端 未结 2 1866
误落风尘
误落风尘 2021-02-04 08:12

I would like to consider best-selling product in this loop :

 \'product\', \'posts_per_page\' => 1, \'product_         


        
2条回答
  •  臣服心动
    2021-02-04 08:37

    I had a hard time trying to get a list of best selling product through the standard loop. But none of the solutions from here and other stackoverflow posts with the same problem do not work for me. Finally I get result that solve my problem through the custom query to data base.

    global $wpdb;
    
    
    $order_totals = $wpdb->get_results( "SELECT kp_posts.ID FROM kp_posts  INNER JOIN kp_postmeta ON ( kp_posts.ID = kp_postmeta.post_id ) WHERE 1=1 AND ( kp_postmeta.meta_key = 'total_sales' ) AND kp_posts.post_type = 'product' AND (kp_posts.post_status = 'publish') GROUP BY kp_posts.ID ORDER BY kp_postmeta.meta_value+0 DESC, kp_posts.post_date DESC LIMIT 0, 16" );
    
    
    foreach ( $order_totals as $value ) {
        $product = wc_get_product( $value->ID );
    
        echo $product->get_title();
    }
    

提交回复
热议问题