我想为Woo数据库模式构建一个MySQL查询,以获得简单/可变产品,但以特定顺序:我想以ID
ASC
结束顺序获得它们,但是当变量产品满足时,我想获得完全低于其父产品的变化。
所以对于一个简单的产品,或者一个可变产品的父产品,post_type
字段设置为"product",post_parent
字段设置为0。对于变量产品的子产品(又名变体),post_type
字段设置为'variable_product',post_parent
字段设置为父产品的ID
。
那么,想象一下期望的顺序:
<表类>
ID
post_title
post_type
post_parent
tbody><<tr>1100 title1 产品 0 1104 title2 产品 0 1130 title2——variation1 variable_product 1104 1200 title2——variation2 variable_product 1104 1208 title2——variation3 variable_product 1104 1107 title3 产品 0 1111 title4 产品 0 1205 title4——variation1 variable_product 1111 1210 title4——variation4 variable_product 1111 1430 title4——variation3 variable_product 1111 1432 title4——variation2 variable_product 1111 表类>
可以使用以下查询。它可能符合你的要求。
$args = array(
'post_per_page' => 1,
'order' => 'DESC',
'post_type' => ['product', 'variable_product'],
);
$products = get_children($args);
if ($products) {
foreach ($products as $product) {
}
}