How to get the list of WooCommerce product image of a certain category from database?

The post thumbnail is not stored as a URL. The post thumbnail is stored as the attachment ID in the _thumbnail_id post meta. The actual file is stored as the _wp_attached_file post meta for that attachment post. WordPress does not store the full URL for attachments in the database. It only stores the path to the file, relative to the uploads directory.

This query will list product IDs and the path to their thumbnail files. You will then need to append those to the URL to your uploads directory:

SELECT 
    p.ID,
    am.meta_value
FROM
    wp_posts p
LEFT JOIN
    wp_postmeta pm ON 
        pm.post_id = p.ID AND
        pm.meta_key = '_thumbnail_id'
LEFT JOIN
    wp_postmeta am ON
        am.post_id = pm.meta_value AND
        am.meta_key = '_wp_attached_file'
WHERE
    p.post_type="product" AND
    p.post_status="publish"

Since the site URL and uploads directory are dynamic, and can be controlled via PHP, it doesn’t make sense to store the full URL. WordPress uses PHP to determine what the URL to the file is, based the site configuration and the relative path to the file. It also doesn’t make sense to store the URL, because some operations require the path, not the URL.

Also note that the only reason you’d do this with SQL is if you were accessing the file paths outside of WordPress, and not using the REST API. If you’re inside a WordPress/WooCommerce template, then the correct way to display the product image is:

$product = wc_get_product( $product_id );

echo $product->get_image( 'full' );

Leave a Comment