我如何获取/展示 WooCommerce 畅销产品

Meh*_*ruf 5 php wordpress product woocommerce

我正在开发一个 WooCommerce 电子书商店,我想在我的主页上显示畅销产品列表。

我怎样才能做到这一点?我可以使用自定义 WP_Query 来做到这一点吗?还有其他办法吗?

Loi*_*tec 5

要获得畅销产品,您可以使用不同的方式:

WordPress WP_Query (仅限 20 个畅销产品)

$query_args =  array(
   'posts_per_page' =>  20, // -1 (for all)
   'post_type'      =>  array( 'product' ),
   'post_status'    =>  'publish',
   'meta_key'       => 'total_sales',
   'order'          => 'DESC',
   'orderby'        => 'meta_value_num',
);

$query = new WP_Query( $query_args );

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

// Your product data
the_title(); // Output product title

endwhile;
wp_reset_postdata();

else :
// No post found

endif;
Run Code Online (Sandbox Code Playgroud)

WooCommerce Shorcode (20 个最畅销的产品)

[products best_selling limit="20"]
Run Code Online (Sandbox Code Playgroud)

文档: