I am trying retrieve wordpress postmeta USING PHP not WORDPRESS functions, it needs to be in php no wordpress functions please.

this is what I have:

$resultat = mysqli_query($con,"SELECT * FROM wp_posts 
                                 LEFT JOIN wp_postmeta 
                                 ON wp_posts.ID= wp_postmeta.post_id 
                                 where post_type='shop_order'");

the issue here is each meta key is in its own row so this doesn't work :/ any suggestions please?

what I figured could be done is to get all the post ids first , store then in array then retrieve the values of each? let me know what you think

  • Not sure why your query doesn't work - what are you expecting the output to be? In a Wordpress database, each piece of meta data is stored on its own row. The only thing I would suggest is doing a RIGHT JOIN to avoid some nulls – Khan Jul 14 '14 at 21:23

The reverse mechanism should work. I assume you are trying the get post metadata with the post row.

$resultat = mysqli_query($con,"SELECT * FROM wp_postmeta 
                                 LEFT JOIN wp_post 
                                 ON wp_postmeta.post_id = wp_post.ID 
                                 WHERE wp_post.post_type='shop_order'");

However, there will be a lots of duplication. If one post have 100 metadata, you are also getting the post row 100 times.

  • that returns an error saying its a boolean value – cppit Jul 14 '14 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.