我试图使用自定义查询基于WooCommerce网站上的总销售额列出10种热门产品。但是必须将零评论/无评论的同一时间产品从列表中排除。有什么方法可以做到这一点?
以下是代码:
<?php
global $woocommerce;
global $product;
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num'
);
$popular_products = new WP_Query($args);
if($popular_products->have_posts()):
?>
<ul>
<?php
while ($popular_products->have_posts()) : $popular_products->the_post();
$product = wc_get_product( get_the_ID() );
$rating = round($product->get_average_rating());
?>
<li>
<h3><?php the_title(); ?></h3>
<div><?php the_post_thumbnail(); ?></div>
<p><strong>Rating: </strong><?php echo $rating; ?>/5</p>
<a href="<?php the_permalink(); ?>">View Product</a>
</li>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
<?php endif; ?>
我开始了解“ post__not_in”,但不确定如何将其与产品等级一起使用。任何帮助,将不胜感激。
以下内容还将排除零评论/无评论的产品:
<?php
$products = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 10,
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'meta_query' => array( array(
'key' => '_wc_review_count',
'value' => '0',
'compare' => '>',
)),
));
if( $products->have_posts() ):
?>
<ul>
<?php
while ( $products->have_posts() ) : $products->the_post();
$_product = wc_get_product( get_the_ID() );
$rating = round( $_product->get_average_rating() );
?>
<li>
<h3><?php the_title(); ?></h3>
<div><?php the_post_thumbnail(); ?></div>
<p><strong>Rating: </strong><?php echo $rating; ?>/5</p>
<a href="<?php the_permalink(); ?>">View Product</a>
</li>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
<?php endif; ?>
请注意,global $woocommerce;
和global $product;
不需要......
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句