You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将无/零价格商品排到WooCommerce分类列表末尾

解决WooCommerce跨分页零价格商品后置问题

原代码的问题在于使用the_posts钩子仅对当前分页的商品数组进行重排,导致无价格/零价格商品会分散在每一页的末尾,无法实现全局范围内所有有价格商品在前、无价格商品在后的效果。

要实现全局排序,需要在数据库查询阶段就调整排序规则,让WordPress先按商品价格是否有效(>0)进行分组排序,再执行默认的排序逻辑。以下是修改后的代码:

add_action( 'pre_get_posts', 'global_sort_zero_price_products_to_end' );

function global_sort_zero_price_products_to_end( $query ) {
    // 仅针对前台的商店页面、商品分类页面生效,且排除后台查询
    if ( ! is_admin() && $query->is_main_query() && ( is_shop() || is_product_category() ) ) {
        // 保留原有的排序参数(比如用户选择的价格/销量排序)
        $orderby = $query->get( 'orderby' );
        $order = $query->get( 'order' );

        // 设置首要排序规则:先按价格是否大于0分组,再按原有规则排序
        $query->set( 'meta_query', array(
            'relation' => 'OR',
            // 有价格的商品:_price > 0
            'price_exists' => array(
                'key'     => '_price',
                'value'   => 0,
                'type'    => 'DECIMAL',
                'compare' => '>'
            ),
            // 无/零价格的商品:_price <= 0 或不存在该元数据
            'price_missing_or_zero' => array(
                'key'     => '_price',
                'value'   => 0,
                'type'    => 'DECIMAL',
                'compare' => '<='
            )
        ) );

        // 设置排序顺序:先按meta_query的分组排序(price_exists在前),再按原有排序规则
        $query->set( 'orderby', array(
            'price_exists' => 'DESC',
            $orderby => $order
        ) );
    }
}

代码说明:

  • 使用pre_get_posts钩子在查询执行前修改参数,从数据库层面控制全局排序,分页逻辑会自动遵循这个排序规则
  • 通过meta_query将商品分为两组:有有效价格的商品(_price>0)和无/零价格的商品(_price<=0)
  • 排序时优先按分组规则排列(有价格组在前),再保留用户原本选择的排序方式(比如价格升序、销量降序等)
  • 仅针对前台主查询生效,不会影响后台的商品管理页面

注意事项:

  • 确保你的WooCommerce商品价格存储在_price元字段中(这是WooCommerce的默认设置)
  • 如果需要调整排序的次要规则,可以修改$orderby和$order的处理逻辑

内容的提问来源于stack exchange,提问作者ahmadwp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 15:25:27