wp query – How to get around missing meta in queries
Question
I have a query something like
//read products with thumbnail and sku
SELECT products.*, pm1.meta_value as sku, p1.guid as thumbnail
FROM `wp_posts` as products
LEFT JOIN `wp_postmeta` as pm1 ON products.product_id = pm1.post_id
LEFT JOIN `wp_postmeta` as pm2 ON products.product_id = pm2.post_id
LEFT JOIN `wp_posts` as p1 ON pm2.meta_value = p1.ID
WHERE products.post_type="product" AND pm1.meta_key = '_sku' AND pm2.meta_key = '_thumbnail_id';
This Query will get all the products that have a sku and a thumbnail but will not get any products who are missing a sku or thumbnail.
How do I get all products and the sku if it exists and the thumbnail if it exists?
I do not want to have to run 3 queries and merge the data.
0
9 months
2022-04-21T12:56:14-05:00
2022-04-21T12:56:14-05:00 0 Answers
0 views
0
Leave an answer