[英]Order by custom Woocommerce product sorting in a WP_Query
I have created a shortcode to display products by category with the query below: 我创建了一个简码以通过以下查询按类别显示产品:
$atts = shortcode_atts( array (
'type' => 'product',
'posts' => -1,
'category' => '',
), $atts, 'list_products' );
$query = new WP_Query( array(
'post_type' => $atts['type'],
'posts_per_page' => $atts['posts'],
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category'],
) ),
) );
This is all fine, however I am trying to use the order by attribute when I sort the products using the custom sort shown here 一切都很好,但是当我使用此处显示的自定义排序对产品进行排序时,我尝试使用order by属性
I have added: 'orderby' => 'modified',
and tried some others from here . 我添加了: 'orderby' => 'modified',
并从这里 尝试了其他一些。
Two questions: 两个问题:
Which attribute do I need to use to sort when using custom sorting 使用自定义排序时,我需要使用哪个属性来进行排序
And 和
What do I need to do to make my orderby
attribute work when added to the above query? 将我的orderby
属性添加到上述查询后,该怎么做? Because it which ever attribute value I use it always sorts by published descending. 因为我使用过的哪个属性值始终按发布的降序进行排序。
When you use custom sorting for Woocommerce products, it set some values for each product in wp_posts
database table under menu_order
column. 当对Woocommerce产品使用自定义排序时,它将在menu_order
列下的wp_posts
数据库表中为每种产品设置一些值。
Then you just need to add 'orderby' => 'menu_order',
in your WP_Query
, so in your code: 然后你只需要添加'orderby' => 'menu_order',
在你WP_Query
,所以在你的代码:
$atts = shortcode_atts( array (
'type' => 'product',
'posts' => -1,
'category' => '',
), $atts, 'list_products' );
$query = new WP_Query( array(
'post_type' => $atts['type'],
'posts_per_page' => $atts['posts'],
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category'],
) ),
'orderby' => 'menu_order',
// 'order' => 'DESC',
) );
It will work (by default the order
sorting argument is set to ASC
normally) . 它将起作用(默认情况下, order
排序参数通常设置为ASC
) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.