post meta - Display multiple meta_keymeta_values by single SQL query

admin2025-06-02  1

This is my query

SELECT P.ID, P.post_title, P.post_content, P.post_author,
MAX(IF(PM.meta_key = 'ex_latitude', PM.meta_value, NULL)) AS lat,
MAX(IF(PM.meta_key = 'ex_longitude', PM.meta_value, NULL)) AS lng    

FROM wp_posts AS P
LEFT JOIN wp_postmeta AS PM on PM.post_id = P.ID
WHERE P.post_type = 'post' and P.post_status = 'publish'
ORDER BY P.ID DESC   

Result only one post data row

+-----+------------+--------------+-------------+-----------+---------------+
| ID  | post_title | post_content | post_author |    lat    |      lng      |
+-----+------------+--------------+-------------+-----------+---------------+
| 10  | Post Title | Post Content |           1 | 56.00078  | 26.45677      |
+-----+------------+--------------+-------------+-----------+---------------+

But how return all posts?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748815857a313955.html

最新回复(0)