WordPress数据库:在mysql查询中包含自定义字段

时间:2016-08-26 21:04:33

标签: mysql wordpress custom-post-type custom-fields

SELECT DISTINCT    
            city_latitude.post_id,
            city_latitude.meta_value as charity_latitude,
            city_longitude.meta_value as charity_longitude,
            ((ACOS(SIN(%f * PI() / 180) * SIN(city_latitude.meta_value * PI() / 180) + COS(%f * PI() / 180) * COS(city_latitude.meta_value * PI() / 180) * COS((%f - city_longitude.meta_value) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance,
            FxFfekP0_posts.post_title
            FROM 
            FxFfekP0_postmeta AS city_latitude
            LEFT JOIN FxFfekP0_postmeta as city_longitude ON city_latitude.post_id = city_longitude.post_id
            INNER JOIN FxFfekP0_posts ON FxFfekP0_posts.ID = city_latitude.post_id
            WHERE city_latitude.meta_key = 'latitude' AND city_longitude.meta_key = 'longitude'
            HAVING distance < %d
            ORDER BY distance ASC

如何添加到此查询并包含charity_name的自定义字段作为列?我很难用这个......

注意:我确实在wordpress.stackoverflow.com发布了此消息,但我建议将其发布在此处,因为它更像是一个MySQL问题。

1 个答案:

答案 0 :(得分:0)

未经测试,但应该是正确的 - 搜索此网站是否有任何不立即有意义的内容......

SELECT lat.post_id
     , lat.meta_value charity_latitude
     , lon.meta_value charity_longitude
     , name.meta_value charity_name
     , geo_distance_km(user_lat,user_lon,lat.meta_value,lon.meta_value) distance
     , p.post_title
  FROM FxFfekP0_postmeta lat
  LEFT 
  JOIN FxFfekP0_postmeta lon 
    ON lon.post_id = lat .post_id
   AND lon.meta_key = 'longitude'
  LEFT
  JOIN FxFfekP0_posts p
    ON p.ID = lat.post_id
  LEFT
  JOIN FxFfekP0_postmeta name
    ON name.post_id = lat.post_id
   AND name.meta_key = 'charity_name'
 WHERE lat.meta_key = 'latitude' 
AND geo_distance_km(user_lat,user_lon,lat.meta_value,lon.meta_value) < %d
        ORDER BY distance ASC