Get url of product’s images (woocommerce)

What happen when a post (product) didnt have a guid,because it has the same image with another product ?

For example:

Query1:

select guid from wp_posts where post_type="attachment" and post_parent=num 

Returns nothing.

Query2:

select guid from wp_posts where ID=num

Returns the url of product.

In this case how should i find the image url of that product?

3 s
3

Firstly, the GUID is not the URL. It is a “globally unique identifier”. WordPress uses the URL for this purpose, but accessing it is not a reliable way to get the URL for anything (this was a bad idea, but is kept this way for backwards compatibility).

It’s not possible to query the image URL from the database directly. The full URL is not stored there. But if you have the ID for a product and want to get the URL for its image you can do this:

$product   = wc_get_product( $product_id );
$image_id  = $product->get_image_id();
$image_url = wp_get_attachment_image_url( $image_id, 'full' );

Leave a Comment