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?
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?
Share Improve this question edited May 21, 2019 at 15:23 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked May 21, 2019 at 14:48 user8761967user8761967 1011 gold badge1 silver badge3 bronze badges3 Answers
Reset to default 16Firstly, 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' );
i have seen some ware a query to fetch products image from database.
may be it will help you.
just set required parameters as per your requirement.
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';
For your reference just run this query in database :
SELECT * FROM `wp_postmeta` WHERE meta_key IN ('_wp_attached_file', '_wp_attachment_backup_sizes', '_wp_attachment_metadata', '_thumbnail_id')
And if you run the above query with "_wp_attached_file" only you will get the path from upload directory.
Hope it helps..