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

如何在WooCommerce中筛选仅含Simple类型产品的产品分类?

解决方法:获取包含Simple类型产品的WooCommerce产品分类及对应数量

问题根源

你之前的tax_query用法有误,get_categories()(或更规范的get_terms())的tax_query是用来筛选分类自身的关联条件,无法直接关联产品的product_type属性。要筛选出包含Simple产品的分类,需要先定位所有关联了Simple产品的产品分类,再获取这些分类的信息并统计对应数量。


方案一:WP_Query实现(逻辑清晰,适合新手)

通过两次查询完成需求:先获取所有Simple产品的关联分类ID,再获取分类详情并统计每个分类下的Simple产品数量。

// 1. 查询所有已发布的Simple类型产品,仅返回ID提升效率
$simple_products = new WP_Query([
    'post_type'      => 'product',
    'posts_per_page' => -1,
    'post_status'    => 'publish',
    'tax_query'      => [
        [
            'taxonomy' => 'product_type',
            'field'    => 'slug',
            'terms'    => 'simple'
        ]
    ],
    'fields'         => 'ids'
]);

if ($simple_products->have_posts()) {
    // 2. 提取所有关联的产品分类ID并去重
    $cat_ids = [];
    foreach ($simple_products->posts as $product_id) {
        $product_cats = wp_get_post_terms($product_id, 'product_cat', ['fields' => 'ids']);
        $cat_ids = array_merge($cat_ids, $product_cats);
    }
    $cat_ids = array_unique($cat_ids);

    // 3. 获取目标分类的详情
    $target_cats = get_terms([
        'taxonomy'     => 'product_cat',
        'include'      => $cat_ids,
        'hide_empty'   => false,
        'hierarchical' => true,
        'orderby'      => 'name',
        'order'        => 'ASC',
        'child_of'     => 0
    ]);

    // 4. 统计并输出每个分类下的Simple产品数量
    foreach ($target_cats as $cat) {
        $count_query = new WP_Query([
            'post_type'      => 'product',
            'posts_per_page' => -1,
            'post_status'    => 'publish',
            'tax_query'      => [
                'relation' => 'AND',
                [
                    'taxonomy' => 'product_cat',
                    'field'    => 'term_id',
                    'terms'    => $cat->term_id
                ],
                [
                    'taxonomy' => 'product_type',
                    'field'    => 'slug',
                    'terms'    => 'simple'
                ]
            ],
            'fields'         => 'ids'
        ]);
        $simple_count = $count_query->found_posts;

        echo "分类:{$cat->name},Simple产品数量:{$simple_count}<br>";
    }
}

方案二:自定义SQL查询(效率更高,适合大数据量场景)

直接通过关联数据库表查询,一次性获取分类信息和对应Simple产品数量,减少多次查询的开销。

global $wpdb;

$sql = "
    SELECT 
        t.term_id,
        t.name,
        t.slug,
        COUNT(p.ID) AS simple_product_count
    FROM {$wpdb->terms} t
    INNER JOIN {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id
    INNER JOIN {$wpdb->term_relationships} tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
    INNER JOIN {$wpdb->posts} p ON tr.object_id = p.ID
    INNER JOIN {$wpdb->term_relationships} tr_type ON p.ID = tr_type.object_id
    INNER JOIN {$wpdb->term_taxonomy} tt_type ON tr_type.term_taxonomy_id = tt_type.term_taxonomy_id
    INNER JOIN {$wpdb->terms} t_type ON tt_type.term_id = t_type.term_id
    WHERE 
        tt.taxonomy = 'product_cat'
        AND p.post_type = 'product'
        AND p.post_status = 'publish'
        AND tt_type.taxonomy = 'product_type'
        AND t_type.slug = 'simple'
    GROUP BY t.term_id
    ORDER BY t.name ASC
";

$product_cats = $wpdb->get_results($sql);

// 输出结果
foreach ($product_cats as $cat) {
    echo "分类:{$cat->name},Simple产品数量:{$cat->simple_product_count}<br>";
}

注意事项

  • 推荐用get_terms()替代get_categories(),后者是针对默认文章分类category的封装,WooCommerce产品分类product_cat用get_terms()更规范。
  • 若不需要层级显示,可将hierarchical设为false,进一步提升查询效率。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 17:35:42