I'm stuck trying to create a query that returns me the parameters:
ID, post_title, post_content, category, ID_category, SKU, name, stock, price
At this moment I have this:
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
And it is giving me correctly the products and the metadata but it is very difficult for me to get the category ID and the category name in this query and I haven't been able to find information on the net.
Thanks.
You had object_id
joining to term_taxonomy_id
which made no sense.
Here is how I think it should be -- caveat: I've never queried a wp database and was just going by the documentation.
SELECT
p.ID,
p.post_title,
`post_content`,
`post_excerpt`,
t.name AS product_category,
t.term_id AS product_id,
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
JOIN wp_term_taxonomy AS tt ON tt.taxonomy = 'product_cat' AND tt.term_taxonomy_id = tr.term_taxonomy_id
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
FWIW
I started on the same thing myself. And I wanted to add product category and Google led me here.
The following code also shows the variation products, not only the post/product main/root for the variations:
SELECT
post_id,
t.name AS product_category,
IF(p.post_parent = 0, p.ID, p.post_parent) AS post_parent,
(SELECT post_title FROM wp_posts WHERE id = pm.post_id) AS title,
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_price" LIMIT 1) AS price,
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_regular_price" LIMIT 1) AS "regular price",
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_stock" LIMIT 1) AS stock,
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_stock_status" LIMIT 1) AS "stock status",
IFNULL((SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_sku" LIMIT 1),
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_custom_field" LIMIT 1)) as SKU,
IFNULL(SUBSTR( (SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_product_attributes" LIMIT 1),
INSTR((SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_product_attributes" LIMIT 1), "is_variation")+16,1),1) AS "variation"
FROM `wp_postmeta` AS pm
JOIN wp_posts AS p ON p.ID = pm.post_id
JOIN wp_term_relationships AS tr ON tr.object_id = IF(p.post_parent = 0, p.ID, p.post_parent)
JOIN wp_term_taxonomy AS tt ON tt.taxonomy = 'product_cat' AND tt.term_taxonomy_id = tr.term_taxonomy_id
JOIN wp_terms AS t ON t.term_id = tt.term_id
WHERE meta_key in ("_product_version")
AND p.post_status in ("publish")
AND IFNULL(SUBSTR((SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_product_attributes" LIMIT 1),
INSTR((SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_product_attributes" LIMIT 1), "is_variation")+16,1),0)=0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With