I am working on received data from the database. I want this data in an array but the ID and post_title returns more than one in the results.
wp_posts
+----+------------+
| ID | post_title |
| 1 | radio |
| 2 | television |
+----+------------+
wp_postmeta
+---------+---------+----------+------------+
| meta_id | post_id | meta_key | meta_value |
| 1 | 1 | price | 12.50 |
| 2 | 1 | _sku | 5876gh7 |
| 3 | 1 | thumb_id | 56 |
| 4 | 2 | price | 342.50 |
| 5 | 2 | _sku | tv784 |
| 6 | 2 | thumb_id | 78 |
+---------+---------+----------+------------+
$query = "
SELECT p.ID, p.post_title, m.meta_id, m.meta_key, m.meta_value
FROM wp_posts p
LEFT JOIN wp_postmeta m ON (m.post_id = p.ID)
WHERE EXISTS (SELECT 1 FROM wp_posts WHERE post_title LIKE '%rad%' AND ID = p.ID)
OR EXISTS (SELECT 1 FROM wp_postmeta WHERE meta_key = '_sku' AND meta_value LIKE '%rad%' AND post_id = p.ID)
ORDER BY p.ID, m.meta_id
";
$result = mysqli_query($connection, $query);
while($row = $result->fetch_object() ){
$sendback[] = $row;
}
print_r($sendback);
This is the result that I see if I print this. You can see the ID and post_title is showing in every result.
array(
[0] => stdClass Object (
[ID] => 1
[post_title] => Radio
[meta_key] => price
[meta_value] => 12.50
)
[1] => stdClass Object (
[ID] => 1
[post_title] => Radio
[meta_key] => _sku
[meta_value] => 5876gh7
)
[2] => stdClass Object (
[ID] => 1
[post_title] => Radio
[meta_key] => thumb_id
[meta_value] => 56
)
)
Which method can I use to get the ID and post_title once like this
array(
'id' => '1',
'post_title' => 'radio',
'price' => '12.50',
'sku' => '5876gh7',
'thumb_id' => '56'
)