Abu*_*ooh 2 php sorting wordpress product woocommerce
我创建了一个短代码,通过以下查询按类别显示产品:
$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'],
) ),
) );
Run Code Online (Sandbox Code Playgroud)
这一切都很好,但是当我使用此处显示的自定义排序对产品进行排序时,我尝试使用按属性排序
我补充说:'orderby' => 'modified',
并从这里尝试了其他一些。
两个问题:
使用自定义排序时需要使用哪个属性进行排序
和
orderby
添加到上述查询时,我需要做什么才能使我的属性起作用?因为我使用它的哪个属性值总是按发布的降序排序。
当您对 Woocommerce 产品使用自定义排序时,它会wp_posts
在menu_order
列下的数据库表中为每个产品设置一些值。
然后你只需要'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',
) );
Run Code Online (Sandbox Code Playgroud)
它将起作用(默认情况下,order
排序参数设置为ASC
正常)。