When you’re working on a WooCommerce site with loads of products and you know there are some with missing images it’s a drag to have to go through the product list to visually identify which ones they are. So how do you more easily find the products with missing images? If you have access to the database, a much easier way of doing it is to run a little SQL snippet.

Products are located in the wp_posts table with a post_type of ‘product’. Also, you probably only want to get the products that are published, so the post_status for this is ‘publish’.

Products that have featured images set have a corresponding record in the wp_postmeta table with a meta_key of ‘_thumbnail_id’ so we are targeting ones that don’t. We can use NOT EXISTS for this. So you can find these products with missing images by running this script:

SELECT  p.ID,p.post_title
FROM    wp_posts p 
WHERE   p.post_type = 'product' 
AND     p.post_status = 'publish'
AND     NOT EXISTS (SELECT '1' 
                    FROM    wp_postmeta pm 
                    WHERE   pm.post_id = p.ID 
                    AND     pm.meta_key = '_thumbnail_id' )

Getting the data as links to product page

If you want to get the data as full links to the admin pages for these products you can use the following in the select clause:

<yoursiteurl>/wp-admin/post.php?post=<p.ID>&action=edit

So if my site was example.com, the select would look like this:

SELECT  CONCAT('https://example.com/wp-admin/post.php?post=',p.ID,'&action=edit')
FROM    wp_posts p 
WHERE   p.post_type = 'product' 
AND     p.post_status = 'publish'
AND     NOT EXISTS (SELECT '1' 
                    FROM    wp_postmeta pm 
                    WHERE   pm.post_id = p.ID 
                    AND     pm.meta_key = '_thumbnail_id' )

You can paste the output straight into the browser and it will bring up the edit screens for each of the products.

The script that gets the admin URLs uses MySQLs concat function which is not exactly the same as the way as some other databases concatenate. You can read about it here.

Hey there – thanks for reading!
Have I helped you? If so, could you…
Find WooCommerce products with missing images (SQL)

2 thoughts on “Find WooCommerce products with missing images (SQL)

  • September 18, 2022 at 9:03 pm
    Permalink

    Interestingly, I get a different result checking for _wp_attached_file:

    SELECT p.ID,p.post_title
    FROM wp_posts p
    WHERE p.post_type = ‘product’
    AND p.post_status = ‘publish’
    AND NOT EXISTS (SELECT ‘1’
    FROM wp_postmeta pm
    WHERE pm.post_id = p.ID
    AND pm.meta_key = ‘_wp_attached_file’ )

    Reply
    • November 3, 2022 at 1:16 am
      Permalink

      Hi Brad, thanks for the comment! I think _wp_attached_file is different – it’s related to attachments. When I run this SQL I get only attachment as the output:

      select distinct post_type
      from wp_posts p, wp_postmeta m
      where p.id = m.post_id
      and meta_key = ‘_wp_attached_file’;

      If I run this SQL for the WooCommerce placeholder image:

      select id,post_title,guid,meta_value
      from wp_posts p, wp_postmeta m
      where p.id = m.post_id
      and meta_key = ‘_wp_attached_file’
      and id = 6;

      I get this as the output:

      6, woocommerce-placeholder, https://domain.com/wp-content/uploads/2020/09/woocommerce-placeholder.png, woocommerce-placeholder.png

      So seems to me the _wp_attached_file record is just holding the file name for an attachment

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *