【发布时间】:2022-08-17 12:48:26
【问题描述】:
我想在商店页面中对最畅销的产品进行排序 我怎样才能做到这一点?
-
最畅销产品
-
要按销售额排序,只需进入您的设置 - prnt.sc/GiE9-Wq9pKkv
标签: wordpress woocommerce
我想在商店页面中对最畅销的产品进行排序 我怎样才能做到这一点?
标签: wordpress woocommerce
利用总销售额对于'meta_key'和元值编号对于'orderby'
<?php
$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 10,
);
$product_query = new WP_Query( $args );
while ( $product_query ->have_posts() ) : $product_query ->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
为了特定类别或子类别
<?php
$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array($categories_id)
),
)
);
$product_query = new WP_Query( $args );
while ( $product_query ->have_posts() ) : $product_query ->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
为了woocommerce 默认查询
add_filter('woocommerce_get_catalog_ordering_args','woocommerce_catalog_orderby' );
function woocommerce_catalog_orderby( $args ) {
$args['meta_key'] = 'total_sales';
$args['orderby'] = 'meta_value_num';
return $args;
}
【讨论】:
add_filter('woocommerce_get_catalog_ordering_args', 'woocommerce_catalog_orderby' ); function woocommerce_catalog_orderby( $args ) { $args['meta_key'] = 'total_sales'; $args['orderby'] = 'meta_value_num'; return $args; }