You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

如何查询WordPress父分类下的文章及WooCommerce嵌套分类下的产品?

嘿Craig,我来帮你搞定这两个WordPress相关的查询问题,直接上实用的方案:

问题1:如何选择WordPress中属于某个父分类的所有文章?

WordPress里获取某个父分类(包含所有子分类)下的文章,推荐用官方的WP_Query来做,既安全又符合WP的开发规范,当然也可以直接写SQL查询,两种方案都给你:

方案1:使用WP_Query(推荐)

首先你需要知道目标父分类的ID(比如假设父分类ID是5),然后通过get_term_children()获取该父分类下所有子分类的ID,再用这些ID来查询文章:

// 替换成你的父分类ID
$parent_cat_id = 5;
// 获取所有子分类ID,包括父分类本身
$child_cat_ids = get_term_children( $parent_cat_id, 'category' );
// 把父分类ID也加入数组(如果需要包含父分类直接关联的文章)
$child_cat_ids[] = $parent_cat_id;

// 构建查询参数
$args = array(
    'post_type' => 'post', // 如果你要查自定义文章类型,替换成对应的slug
    'post_status' => 'publish',
    'category__in' => $child_cat_ids,
    'posts_per_page' => -1, // 获取所有文章,不要分页
);

// 执行查询
$query = new WP_Query( $args );

// 循环输出文章
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // 这里输出你需要的内容,比如标题
        echo '<h2>' . get_the_title() . '</h2>';
    }
    // 重置查询
    wp_reset_postdata();
} else {
    echo '该分类下没有文章';
}

方案2:直接SQL查询(适合需要批量操作的场景)

如果你需要直接操作数据库,可以用这条SQL语句,记得替换wp_为你的数据库表前缀,以及parent_term_id = 5中的5为你的父分类ID:

SELECT p.*
FROM wp_posts p
JOIN wp_term_relationships tr ON p.ID = tr.object_id
JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wp_terms t ON tt.term_id = t.term_id
WHERE tt.taxonomy = 'category'
AND (
    tt.parent = 5
    OR tt.term_id IN (
        SELECT term_id FROM wp_term_taxonomy WHERE parent = 5 AND taxonomy = 'category'
        UNION ALL
        SELECT term_id FROM wp_term_taxonomy WHERE parent IN (SELECT term_id FROM wp_term_taxonomy WHERE parent = 5 AND taxonomy = 'category') AND taxonomy = 'category'
        -- 如果分类层级更深,可以继续加UNION ALL来嵌套
    )
)
AND p.post_status = 'publish'
AND p.post_type = 'post'
GROUP BY p.ID;
问题2:查询WooCommerce嵌套分类下的所有产品

你的店铺分类是多层嵌套,产品都在最底层,要获取顶级分类(比如Cat 2)下的所有产品,WooCommerce的产品分类是product_cat这个分类法,我们可以用WP_Query或者专门的WC_Product_Query来实现:

方案1:使用WP_Query

只需要在tax_query里设置include_children为true,就能自动包含所有子分类的产品,非常省心:

// 先获取Cat 2的分类ID,你可以通过后台编辑分类时的URL查看,或者用slug获取:
$cat_slug = 'cat-2'; // 替换成你的Cat 2的slug
$cat_term = get_term_by('slug', $cat_slug, 'product_cat');
$cat_id = $cat_term->term_id;

// 构建查询参数
$args = array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'term_id',
            'terms' => $cat_id,
            'include_children' => true, // *关键:包含所有子分类的产品*
        )
    )
);

$query = new WP_Query( $args );

// 循环输出产品
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        global $product;
        echo '<h3>' . $product->get_name() . '</h3>';
        echo '<p>价格:' . $product->get_price_html() . '</p>';
    }
    wp_reset_postdata();
} else {
    echo '该分类下没有产品';
}

方案2:使用WC_Product_Query(WooCommerce专属)

如果你想更贴合WooCommerce的生态,用WC_Product_Query会更合适,用法和WP_Query类似,但专门针对产品:

$cat_slug = 'cat-2';
$cat_term = get_term_by('slug', $cat_slug, 'product_cat');
$cat_id = $cat_term->term_id;

$args = array(
    'status' => 'publish',
    'category' => array( $cat_id ),
    'limit' => -1,
);

$products = new WC_Product_Query( $args );
$products = $products->get_products();

// 遍历产品
foreach ( $products as $product ) {
    echo '<h3>' . $product->get_name() . '</h3>';
    echo '<p>价格:' . $product->get_price_html() . '</p>';
}

需要注意的是,如果你的分类层级非常深,include_children也能自动覆盖所有子层级,不用额外处理,非常方便~

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

火山引擎 最新活动