Robert Granlund
asked on
WordPress post_meta Query and Join tables
Query WordPress database.
I want to query the post_meta table and find the ID's of a product that has a SKU but an empty warehouse location. I can get either or to work but not both. I believe it is because one meta_value is empty and the other has an entry?
How do I join the tables and simplify this query?
I want to query the post_meta table and find the ID's of a product that has a SKU but an empty warehouse location. I can get either or to work but not both. I believe it is because one meta_value is empty and the other has an entry?
How do I join the tables and simplify this query?
SELECT p.ID, mp.post_id, p.post_title, mp.meta_value
FROM wp_posts AS p, wp_postmeta AS mp
WHERE (p.post_status='publish' AND p.post_type = 'product')
AND (p.ID = mp.post_id)
AND (mp.meta_key = '_simple_product_location_field' AND mp.meta_value = '')
AND (mp.meta_key = '_sku' AND mp.meta_value !='');
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
They can't both be true at the time which is why you need an OR statement in there. In simple terms, you can't possibly have the meta_key = _simple_product_location_f ield AND have the meta_key = _sku !
They can't both be true at the time which is why you need an OR statement in there. In simple terms, you can't possibly have the meta_key = _simple_product_location_f
ASKER
@chris, I re-read what I wrote and I was off. What I meant is, I want to get the value of _sku where _simple_product_location_f ield is empty.
ASKER
AND ((mp.meta_key = '_simple_product_location_
I need _simple_product_location_f
Thanks in advance.