In the table below, the meta keys logo_ID and show_logo are associated with each other based on the post_id field. How do I get the value of logo_ID for rows which have a show_logo of 1 using only mysql?
post_id meta_key meta_value
------- ----------- ----------
262 logo_ID 263
262 show_logo 0
260 logo_ID 261
260 show_logo 1
258 logo_ID 259
258 show_logo 1
If it looks familiar it’s because this is from the WP postmeta table. I’m using $wpdb to query the db so everything has to be in mysql (no WP functions anywhere).
You can join the
wp_postmetatable to itself, specifying the value you want in theWHEREclause.If you are dealing with a lot of data, this is fairly fast as it will use indexes on the
wp_postmetatable. You canJOINthewp_postmetamore if you want to get other values as criteria as well, or thewp_poststable to get the other post fields.SQL Fiddle