简体   繁体   English

通过WP_Query中的自定义Woocommerce产品排序排序

[英]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.

相关问题 在Bootstrap 4 Carousel的WP_Query中按自定义Woocommerce产品排序 - Order by custom Woocommerce product sorting in a WP_Query for Bootstrap 4 Carousel Woocommerce wp_query按ID获取订单 - Woocommerce wp_query get order by ID woocommerce自定义过滤产品页面的制作方式,或者如何在woocommerce中正确修补wp_query? - woocommerce custom filtered product page crafting, or how to correctly patch wp_query in woocommerce? WooCommerce 中自定义端点上的 WP_Query 分页 - WP_Query pagination on a custom endpoint in WooCommerce 通过WP_Query中的menu_order对特定的Woocommerce产品变体进行排序 - Sort specific Woocommerce product variations by menu_order in a WP_Query Woocommerce 中具有产品类别的产品变体 WP_Query - Product variation WP_Query with a product category in Woocommerce 在WP_Query中按自定义字段(数字)排序 - Order by custom field (numeric) in WP_Query 限制WP_Query和WooCommerce产品查询中的帖子 - Limit posts on a WP_Query and in WooCommerce product query 尝试在自定义 WP_Query 中获取 woocommerce 订单详细信息时发生错误 - Error occurs when try to get woocommerce order details in a custom WP_Query 在 WP_Query 中获取 WooCommerce 订阅产品类型和特定类别 - Get WooCommerce Subscription product type and specific category in a WP_Query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM