Get image with a sql query
Question
I am working on a system outside WordPress. This system is connected with the same database as my WordPress.
I made a small visual example of the tables below.
wp_posts
+----+----------------+-----------+--------------------+
| ID | post_title | post_type | guid |
| 23 | Skateboard | product | skateboard-red |
| 24 | Bike | product | bike-white |
| 56 | skateboard-img | image | skateboard-red.png |
| 89 | bike-img | image | bike-white.png |
+----+----------------+-----------+--------------------+
wp_postmeta
+---------+---------+----------+------------+
| meta_id | post_id | meta_key | meta_value |
| 145 | 23 | price | 22.50 |
| 146 | 23 | thumb | 56 |
| 147 | 23 | stock | 15 |
| 148 | 24 | price | 429.50 |
| 149 | 24 | thumb | 89 |
| 150 | 24 | stock | 3 |
+---------+---------+----------+------------+
If I want the meta_values from wp_postmeta I can use the JOIN method.
<?php
SELECT wp_posts.ID, wp_posts.post_title, wp_postmeta.meta_value
FROM wp_posts
JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
WHERE post_type='product' and post_title='Skateboard';
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)){
echo $row['ID'];
echo $row['post_title'];
echo $row['meta_value'];
}
?>
Now the data returns
- 23 Skateboard 22.50
- 23 Skateboard 56
- 23 Skateboard 15
How can I get the matched image by this post?
The relation between the post and the image is the wp_postmeta table.
0
php, query
4 years
2020-05-20T18:10:29-05:00
2020-05-20T18:10:29-05:00 0 Answers
78 views
0
Leave an answer