我正在通过自定义 WP_Query 加载可变产品
$args = array(
'post_type' => 'product',
'posts_per_page' => 100,
'product_cat' => 'beast-balls',
'orderby' => 'date',
'order' => 'desc'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="product-node cat-beast-balls">
<?php wc_get_template_part( 'content', 'single-product' ); ?>
</div>
<?php endwhile;
}
wp_reset_postdata();
这似乎工作正常。但是,我正在使用 ajax 重新加载产品,但使用不同的循环,例如这个循环。
$args = array(
'post_type' => 'product',
'posts_per_page' => 100,
'product_cat' => 'beast-balls',
'orderby' => 'price',
'order' => 'asc'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="product-node cat-beast-balls">
<?php wc_get_template_part( 'content', 'single-product' ); ?>
</div>
<?php endwhile;
}
wp_reset_postdata();
不过,我可以注意到在“asc”和“desc”之间顺序颠倒了,所以至少这是有效的。我的问题是 orderby 值似乎没有区别。我怎样才能使循环改变产品是否按日期或价格订购?
谢谢大家!
最佳答案
试试这个:
$args = array(
'post_type' => 'product',
'posts_per_page' => 100,
'product_cat' => 'beast-balls',
'orderby' => 'meta_value_num',
'meta_key' => '_price',
'order' => 'asc'
);
关于php - WooCommerce 似乎只按日期订购而不是价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30978777/