简体   繁体   English

如何找到没有特色图片的 Woocommerce 产品

[英]How to find Woocommerce Products without a Featured Image

I am trying to create a SQL Query that will output the Woocommerce products in my MySQL Database that do not have a featured image assigned to it. I am trying to create a SQL Query that will output the Woocommerce products in my MySQL Database that do not have a featured image assigned to it. As Woo is based on Wordpress, I'm not sure where to get the information from but it looks as if I will need to join some tables up.由于 Woo 基于 Wordpress,我不确定从哪里获取信息,但看起来我需要加入一些表格。

Has anyone out there had experience with this?有没有人有这方面的经验? I have 500 products and all need to have a Featured Image.我有 500 种产品,都需要有特色图片。 I am going through them randomly and assigning each product one, but I need to see how many are left to do and which ones need to be done.我随机浏览它们并为每个产品分配一个,但我需要看看还有多少要做,哪些需要做。

I am unable to sort the Products by Featured Image in the Woocommerce backend so I am hoping it can be achieved via SQL Query.我无法在 Woocommerce 后端对产品进行排序,所以我希望它可以通过 SQL 查询来实现。

It looks as if I have found the right query.看起来好像我找到了正确的查询。 It joins some tables to give me a list of all products and featured images.它加入了一些表格,为我提供了所有产品和特色图片的列表。 I can then sort this list and see what products have NULL Featured Images.然后我可以对这个列表进行排序,看看哪些产品有 NULL 特色图片。 The SQL Query I used is below in case it helps somebody:我使用的 SQL 查询如下,以防它帮助某人:

SELECT p.ID, p.post_title, pm.*
FROM wp_posts p
LEFT JOIN wp_postmeta pm ON p.ID = pm.post_id
    AND pm.meta_key = '_thumbnail_id'
WHERE p.post_type IN ( 'product' )

This would be my query.这将是我的查询。 It will return all post id's ( same as product id's ) that have no image它将返回所有没有图像的帖子 ID(与产品 ID 相同)

select ID FROM wp_posts WHERE ID NOT IN (select post_id from wp_postmeta WHERE meta_key="_thumbnail_id") AND post_type="product"

Create a View called Products_List :创建一个名为Products_List的视图:

SELECT
    `wptt_posts`.`post_title` AS `post_title`,
    `wptt_posts`.`post_name` AS `post_name`,
    `wptt_postmeta`.`meta_value` AS `meta_value`,
    `wptt_posts`.`ID` AS `ID`
FROM
    (
        `wptt_posts`
        JOIN `wptt_postmeta` ON (
            (
                `wptt_posts`.`ID` = `wptt_postmeta`.`post_id`
            )
        )
    )
WHERE
    (
        (
            `wptt_posts`.`post_type` = 'product'
        )
        AND (
            `wptt_postmeta`.`meta_key` = '_sku'
        )
    )
ORDER BY
    `wptt_postmeta`.`meta_value`

Then create another query using Products_List above:然后使用上面的Products_List创建另一个查询:

SELECT
Products_List.ID as "Product ID",
Products_List.meta_value as "SKU",
Products_List.post_name as "Product Name",
Products_List.post_title as "Product Title",
'NO' AS "HAS FEATURED Image"
FROM
    Products_List
WHERE
    ID NOT IN (
        SELECT
            Products_List.ID
        FROM
            Products_List
        LEFT JOIN wptt_postmeta ON Products_List.ID = wptt_postmeta.post_id
        WHERE
            wptt_postmeta.meta_key = '_thumbnail_id'
    )
UNION
SELECT
Products_List.ID as "Product ID",
Products_List.meta_value as "SKU",
Products_List.post_name as "Product Name",
Products_List.post_title as "Product Title",
'YES' AS "HAS FEATURED Image"
        FROM
            Products_List
        LEFT JOIN wptt_postmeta ON Products_List.ID = wptt_postmeta.post_id
        WHERE
            wptt_postmeta.meta_key = '_thumbnail_id'

Another possibility that does not use a NOT IN :另一种不使用 NOT IN 的可能性:

SELECT p.ID, p.post_title 

FROM `wp_posts` as p LEFT OUTER JOIN wp_postmeta pm ON (p.ID=pm.post_id AND pm.meta_key = '_thumbnail_id') 

WHERE p.post_type = 'product' 
AND 
(meta_key IS NULL OR meta_value = "")

is possible whith sku ? sku 有可能吗? ( no only id) (没有唯一的ID)

SELECT p.ID, p.post_title 
FROM `wp_posts` as p 
LEFT OUTER JOIN wp_postmeta pm 
    ON (p.ID=pm.post_id AND pm.meta_key = '_thumbnail_id')
WHERE 
    p.post_type = 'product' 
    AND (meta_key IS NULL OR meta_value = "")

Simple as that.就那么简单。 The following sql returns sku of all products with the default woocommerce image (means all products without image):以下 sql 返回具有默认 woocommerce 图像的所有产品的 sku(表示所有没有图像的产品):

select wp_postmeta.meta_value from wp_postmeta where wp_postmeta.post_id IN (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!='auto-draft') AND wp_postmeta.meta_key = "_sku";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM