php-通过WP_Query中的自定义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'],
        ) ),
    ) );

一切都很好,但是当我使用自定义排序shown here对产品进行排序时,我尝试使用order by属性

添加了:’orderby’=> “修改”,并尝试了here中的其他内容.

两个问题:

使用自定义排序时,我需要使用哪个属性来进行排序

将我的orderby属性添加到上述查询后,该怎么做?因为我使用过的哪个属性值始终按发布的降序进行排序.

解决方法:

当对Woocommerce产品使用自定义排序时,它将在menu_order列下的wp_posts数据库表中为每种产品设置一些值.

然后,您只需要添加’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',
) );

它将起作用(认情况下,订单排序参数通常设置为ASC).

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...