Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

wordpress - Get also WooCommerce product category thumbnail Id using a SQL query

I'm using WordPress with WooCommerce and I have written the following SQL query which gets the product category terms IDs names and slugs:

SELECT t.term_id AS id, t.name AS post_title,t.slug AS post_url
FROM   wp_terms t 
LEFT JOIN wp_term_taxonomy tt ON t.term_id = tt.term_id
WHERE  tt.taxonomy = 'product_cat'
ORDER  BY name

How should I change this SQL query to get also the product category thumbnail Id?

Note: I'm only interested in a SQL query, but not anything else like a WP_Query.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To get additionally the thumbnail ID in your SQL query for WooCommerce product category terms, you can use the following instead:

SELECT t.term_id AS id, t.name AS post_title,t.slug AS post_url, tm.meta_value AS thumb_id 
FROM   wp_terms t 
LEFT JOIN wp_term_taxonomy tt ON t.term_id = tt.term_id
LEFT JOIN wp_termmeta tm ON t.term_id = tm.term_id
WHERE  tt.taxonomy = 'product_cat'
AND tm.meta_key = 'thumbnail_id'
ORDER BY t.name

Tested and works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...