在Woocommerce的2周前按_sale_price_dates_to获取产品ID(orig_offer_product_id除外)

时间:2018-11-28 14:46:01

标签: php mysql wordpress woocommerce

我正在使用woocommerce,并提供woocommerce。 我想获取没有报价的产品ID。我准备了代码,但没有用:( 我在第二个查询中尝试过:SELECTdistinct(wp_postmeta.meta_value)...,但是它也不起作用。

$draftid = $wpdb->get_col('SELECT wp_posts.ID FROM ' . $wpdb->postmeta . ' join ' . $wpdb->posts . ' ON wp_posts.ID = wp_postmeta.post_id 
WHERE wp_posts.post_status="draft" and wp_postmeta.meta_key="_sale_price_dates_to" and wp_postmeta.meta_value != "" and wp_postmeta.meta_value<="' . (time()-1296000) . '"  
EXCEPT SELECT wp_postmeta.meta_value FROM ' . $wpdb->postmeta . ' join ' . $wpdb->posts . ' ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_postmeta.meta_key="orig_offer_product_id" and wp_postmeta.meta_value !="" and wp_posts.post_type="woocommerce_offer"'); print_r($draftid);

但分别第一个查询有效,第二个查询也有效。如果我使用“联合”而不是“除外”,它也可以工作。

知道为什么吗?

1 个答案:

答案 0 :(得分:0)

不做任何保证就尝试以下操作:

global $wpdb;

$draft_id = $wpdb->get_col("
    SELECT p.ID 
    FROM {$wpdb->prefix}posts as p
    JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id 
    WHERE p.post_status = 'draft' 
    AND pm.meta_key = '_sale_price_dates_to' 
    AND pm.meta_value ! = '' 
    AND pm.meta_value <= " . (time()-1296000) . "
    AND p.ID NOT IN (
        SELECT p2.ID 
        FROM {$wpdb->prefix}posts as p2
        JOIN {$wpdb->prefix}postmeta as pm2  ON p2.ID = pm2.post_id 
        AND pm2.meta_key = 'orig_offer_product_id' 
        AND pm2.meta_value ! = ''
        AND p2.post_type = 'woocommerce_offer'
    )
"); 

print_r($draft_id);

我希望它能起作用。