Mysql 通过自定义查询获取产品

Mysql 通过自定义查询获取产品,mysql,wordpress,woocommerce,Mysql,Wordpress,Woocommerce,这是基于WordPress和WooCommerce以及post和post_元表的交易 我必须获得所有具有meta_key=“\u shop”和meta_value=“yes”的产品ID 典型情况: 如果元键和元值与父产品匹配,则其所有变化也必须包括在内 如果元键和元值未与父项一起移动,但其变体具有匹配的元键和元值,则仅返回变体 Product(#18)(simple): _shop = 'yes' Product(#19)(variable): _shop =

这是基于WordPress和WooCommerce以及post和post_元表的交易

我必须获得所有具有meta_key=“\u 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'
我需要的身份证:

#18 #19 #20 #21 #22 #23 #26
谢谢你的建议。但是有两种典型的情况。你能再清楚一点典型的情况吗。它基于WordPress和WooCommerce。获取所有产品ID,其元键为'shop',元值为'yes'。如果产品类型为变量,则检查其元键和值是否匹配,然后返回该类型下的所有变量。这意味着,如果产品类型=变量,元键='shop',元值='yes',则返回所有产品类型。返回其所有变体,如果父项不匹配,则检查其变体,如果变体匹配,则返回变体的ID谢谢您的建议。有两种情况,如果父产品与方案匹配,则返回所有变体,否则仅返回变体。您根据什么确定变体的父产品?比如25岁的父母是24岁?
    select  productID from your_table
   where meta_key = "_shop" and meta_value = "yes"
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;
        }
    }
});