我卡試圖創建一個返回我參數的查詢:SQL查詢來獲取所有的產品,類別和元數據woocommerce/WordPress的
ID,POST_TITLE,POST_CONTENT,類別,ID_category,SKU,名稱,股票,價格
在這一刻我有這樣的:
SELECT
p.ID,
p.post_title,
`post_content`,
`post_excerpt`,
t.name AS product_category,
t.slug AS product_slug,
tt.term_taxonomy_id AS tt_term_taxonomia,
tr.term_taxonomy_id AS tr_term_taxonomia,
MAX(CASE WHEN pm1.meta_key = '_price' then pm1.meta_value ELSE NULL END) as price,
MAX(CASE WHEN pm1.meta_key = '_regular_price' then pm1.meta_value ELSE NULL END) as regular_price,
MAX(CASE WHEN pm1.meta_key = '_sale_price' then pm1.meta_value ELSE NULL END) as sale_price,
MAX(CASE WHEN pm1.meta_key = '_sku' then pm1.meta_value ELSE NULL END) as sku
FROM wp_posts p LEFT JOIN wp_postmeta pm1 ON (pm1.post_id = p.ID)
LEFT JOIN wp_term_relationships AS tr ON (tr.object_id = p.ID)
LEFT JOIN wp_term_taxonomy AS tt ON (tt.taxonomy = 'product_cat' AND tr.object_id = tt.term_taxonomy_id)
LEFT JOIN wp_terms AS t ON (t.term_id = tt.term_id)
WHERE p.post_type in('product', 'product_variation') AND p.post_status = 'publish' AND p.post_content <> ''
GROUP BY p.ID,p.post_title
而且它正確地給我的產品和元數據,但它是非常困難的,我讓在這個類別ID和類別名稱查詢和我一直無法在網上找到信息。
謝謝。
這裏是數據庫模型 - 哪裏就是一個名爲Category ID的列? https://github.com/woocommerce/woocommerce/wiki/Database-Description - 你想要哪些列? – Hogan
嗨@霍根,謝謝你的回覆。所有產品信息都作爲文章存儲在Wordpress數據庫中。 https://codex.wordpress.org/Database_Description – superTramp
那麼,所有的術語/分類信息都可以在'wp_terms'中,也可以在'wp_term_taxonomy'和'wp_term_relationships'中。你對這個查詢的推理是什麼?有可能比純SQL查詢更好。 WordPress有自己的功能(如'get_posts()'),WooCommerce有一個[API](https://woocommerce.github.io/woocommerce-rest-api-docs/) – helgatheviking