1

This is my table. I simply would like one select statement that returns coupon_code, max_uses, and times_used. I don't know how to join it all to do that.

mysql> select * from wp_postmeta where post_id = '207717';
+---------+---------+------------------------+-------------------------------+
| meta_id | post_id | meta_key               | meta_value                    |
+---------+---------+------------------------+-------------------------------+
|  795679 |  207717 | coupon_code            | emeraldcity                   |
|  795689 |  207717 | max_uses               | 30                            |
|  795699 |  207717 | start_date             | 2016-07-22                    |
|  795704 |  207717 | _end_date              | WPMUDEV_Field_Datepicker      |
|  795705 |  207717 | _edit_lock             | 1492003198:1                  |
|  913311 |  207717 | times_used             | 22                            |
+---------+---------+------------------------+-------------------------------+

For example, this gets me just max_uses.

mysql> select * from wp_postmeta where post_id = '207717' and meta_key = 'max_uses';
+---------+---------+----------+------------+
| meta_id | post_id | meta_key | meta_value |
+---------+---------+----------+------------+
|  795689 |  207717 | max_uses | 30         |
+---------+---------+----------+------------+
1

If you use get_post_meta() with just the first parameter ( post ID ) you'll get an array of all the post meta for the passed ID. You'll get more than you expect but you can then use any number of ways to get the data that you want out.

$post_meta = get_post_meta( 207717 )

0

You can use IN Clause

mysql> select * from wp_postmeta where post_id = '207717' and meta_key IN ( 'max_uses', 'coupon_code');
+---------+---------+-------------+-------------+
| meta_id | post_id | meta_key    | meta_value  |
+---------+---------+-------------+-------------+
|  795679 |  207717 | coupon_code | emeraldcity |                   
|  795689 |  207717 | max_uses    | 30          |
+---------+---------+-------------+-------------+

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.