通过SQL检索所有WooCommerce产品,其中没有图像,这些图像都没有

时间:2019-05-06 01:41:14

标签: php mysql sql wordpress woocommerce

我使用此查询sql检索没有图像的所有产品

SELECT id
FROM   `wp_posts` 
WHERE  id NOT IN (SELECT post_id FROM `wp_postmeta` WHERE  `meta_key` = 
'_thumbnail_id') 
AND `post_type` = 'product' 
AND `post_status` = 'publish'

现在,我将检索所有没有图像和库存状态的产品,是否有办法?

1 个答案:

答案 0 :(得分:1)

以下SQL查询将允许您检索没有“现货”图像的产品:

SELECT ID
FROM wp_posts p
INNER JOIN  wp_postmeta pm ON p.ID = pm.post_id
WHERE ID NOT IN (SELECT post_id FROM wp_postmeta WHERE meta_key = '_thumbnail_id')
AND p.post_type = 'product'
AND p.post_status = 'publish'
AND pm.meta_key = '_stock_status'
AND pm.meta_value = 'instock'

或者您可以使用WPDB类通过php来查询它,例如:

global $wpdb;

$product_ids = $wpdb->get_col( "
    SELECT ID
    FROM {$wpdb->prefix}posts p
    INNER JOIN  {$wpdb->prefix}postmeta pm ON p.ID = pm.post_id
    WHERE ID NOT IN (SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_thumbnail_id')
    AND p.post_type = 'product'
    AND p.post_status = 'publish'
    AND pm.meta_key = '_stock_status'
    AND pm.meta_value = 'instock'
");

// Raw output
print_r($product_ids);