查询以父子顺序获取Woo产品



我想为Woo数据库模式构建一个MySQL查询,以获得简单/可变产品,但以特定顺序:我想以IDASC结束顺序获得它们,但是当变量产品满足时,我想获得完全低于其父产品的变化。

所以对于一个简单的产品,或者一个可变产品的父产品,post_type字段设置为"product",post_parent字段设置为0。对于变量产品的子产品(又名变体),post_type字段设置为'variable_product',post_parent字段设置为父产品的ID

那么,想象一下期望的顺序:

<表类> ID post_title post_type post_parent tbody><<tr>1100title1产品01104title2产品01130title2——variation1variable_product11041200title2——variation2variable_product11041208title2——variation3variable_product11041107title3产品01111title4产品01205title4——variation1variable_product11111210title4——variation4variable_product11111430title4——variation3variable_product11111432title4——variation2variable_product1111

可以使用以下查询。它可能符合你的要求。

$args = array(
'post_per_page' => 1,
'order'         => 'DESC',
'post_type'     => ['product', 'variable_product'],
);
$products = get_children($args);
if ($products) {
foreach ($products as $product) {
}
}

最新更新