这是基于WordPress和WooCommerce并处理post和post_meta表
我必须获取所有具有meta_key =“ _shop”和meta_value =“ yes”的产品ID。
典型情况:
如果元键和元值与父产品匹配,则其所有变体也必须包括在内。
如果元键和元值未与父级同步,但其变体具有元键,并且元值匹配,则仅返回变体。
Product(#18)(simple): _shop = 'yes'
Product(#19)(variable): _shop = 'yes' (Parent is Yes)
Product(#20)(variation): _shop = 'yes'
Product(#21)(variation): _shop = 'no'
Product(#22)(variation): _shop = 'no'
Product(#23)(simple): _shop = 'yes'
Product(#24)(variable): _shop = 'no' (parent is no)
Product(#25)(variation): _shop = 'no'
Product(#26)(variation): _shop = 'yes' (Only this variation)
Product(#20)(variation): _shop = 'no'
我需要的ID:
#18#19#20#21#22#23#26
答案 0 :(得分:0)
select productID from your_table
where meta_key = "_shop" and meta_value = "yes"
答案 1 :(得分:0)
add_action('init',function(){
global $wpdb;
$ids = $wpdb->get_results("
SELECT p.ID as id
From $wpdb->posts as p
LEFT JOIN $wpdb->postmeta as m
ON p.ID = m.post_id
WHERE m.meta_key = '_shop'
AND m.meta_value = 'yes'
AND p.post_type = 'product' OR p.post_type = 'product_variation'
");
if(!empty($ids)){
foreach ($ids as $id) {
echo $id->id;
}
}
});