I'm having these two tables:
wp_posts
ID | post_title | post_type |
---|---|---|
1 | Foo | product |
2 | Bar | product |
3 | Test | product |
wp_postmeta
ID | post_id | meta_key | meta_value |
---|---|---|---|
150 | 1 | stock_ny | 150 |
151 | 1 | stock_la | 70 |
152 | 1 | stock_den | 90 |
153 | 1 | blah_blah | asdf |
160 | 1 | blah_blah2 | dfdfdf |
175 | 2 | stock_ny | 80 |
185 | 2 | stock_la | 20 |
195 | 2 | stock_den | 0 |
200 | 2 | blah_blah | asdf |
210 | 2 | blah_blah2 | dfdfdf |
220 | 3 | stock_ny | 86 |
230 | 3 | stock_la | 100 |
240 | 3 | stock_den | 99 |
250 | 3 | blah_blah | gddj |
260 | 3 | blah_blah2 | jdfdfj |
I'm calling it via PHP and WordPress. I've made it this far (not very long, I know):
<?php
$query = 'SELECT ID, post_title, post_type';
$query .= 'FROM wp_posts';
$query .= 'WHERE post_type = "product" LIMIT 10';
$products = $wpdb->get_results( $query, OBJECT );
echo '<pre>';
print_r($products);
echo '</pre>';
?>
And I would like an output like this:
[1] => stdClass Object
(
[id] => 1
[post_title] => Foo
[post_type] => product
[stocks] => [
[stock_ny] => 150,
[stock_la] => 70,
[stock_den] => 90
]
),
[2] => stdClass Object
(
[id] => 2
[post_title] => Bar
[post_type] => product
[stocks] => [
[stock_ny] => 80,
[stock_la] => 20,
[stock_den] => 0
]
),
[3] => stdClass Object
(
[id] => 3
[post_title] => Test
[post_type] => product
[stocks] => [
[stock_ny] => 86,
[stock_la] => 100,
[stock_den] => 99
]
)
I assume it's something with a join?