最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - Get url of product's images (woocommerce)

programmeradmin1浏览0评论

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 badges
Add a comment  | 

3 Answers 3

Reset to default 16

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' );

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..

发布评论

评论列表(0)

  1. 暂无评论