mysql - Subquery with Wordpress posts and post_meta -
i know not right, although working.
i need find posts (as revenue) near given radius , location, need subquery latitude , longitude info in post_meta.
it's gonna hurt eyes, here is:
/* lat -23.6480147 lng -46.704116399999975 radius 20km 6371 para km e 3959 para miles */ select vision_outlet.wp_posts.id 'id', vision_outlet.wp_posts.post_title 'nome', ( select vision_outlet.wp_postmeta.meta_value vision_outlet.wp_postmeta vision_outlet.wp_postmeta.meta_key = 'formatted_address' , vision_outlet.wp_postmeta.post_id = vision_outlet.wp_posts.id ) 'formatted_address' , ( select vision_outlet.wp_postmeta.meta_value vision_outlet.wp_postmeta vision_outlet.wp_postmeta.meta_key = 'lat' , vision_outlet.wp_postmeta.post_id = vision_outlet.wp_posts.id ) 'lat', ( select vision_outlet.wp_postmeta.meta_value vision_outlet.wp_postmeta vision_outlet.wp_postmeta.meta_key = 'lng' , vision_outlet.wp_postmeta.post_id = vision_outlet.wp_posts.id ) 'lng', ( 6371 * acos( cos( radians(-23.6480147) ) * cos( radians ( ( select vision_outlet.wp_postmeta.meta_value vision_outlet.wp_postmeta vision_outlet.wp_postmeta.meta_key = 'lat' , vision_outlet.wp_postmeta.post_id = vision_outlet.wp_posts.id ) ) ) * cos( radians ( ( select vision_outlet.wp_postmeta.meta_value vision_outlet.wp_postmeta vision_outlet.wp_postmeta.meta_key = 'lng' , vision_outlet.wp_postmeta.post_id = vision_outlet.wp_posts.id ) ) - radians(-46.704116399999975) ) + sin( radians(-23.6480147) ) * sin( radians ( ( select vision_outlet.wp_postmeta.meta_value vision_outlet.wp_postmeta vision_outlet.wp_postmeta.meta_key = 'lat' , vision_outlet.wp_postmeta.post_id = vision_outlet.wp_posts.id ) ) ) ) ) 'distance' vision_outlet.wp_posts, vision_outlet.wp_postmeta vision_outlet.wp_posts.id = vision_outlet.wp_postmeta.post_id , vision_outlet.wp_posts.post_status = 'publish' , vision_outlet.wp_postmeta.meta_key = 'formatted_address' having distance < 20 order distance
the question is: should need consider creating new table or there way refactor this?
now it's better:
/* lat -23.6480147 lng -46.704116399999975 radius 20km 6371 para km e 3959 para miles */ select wp_posts.id, wp_posts.post_title, pm1.meta_value formatted_address, pm2.meta_value lat, pm3.meta_value lng, 6371 * acos( cos( radians(-23.6480147) ) * cos( radians( pm2.meta_value ) ) * cos( radians ( pm3.meta_value ) - radians(-46.704116399999975) ) + sin( radians(-23.6480147) ) * sin( radians ( pm2.meta_value ) ) ) 'distance' wp_posts left join wp_postmeta pm1 on (wp_posts.id = pm1.post_id , pm1.meta_key='formatted_address') left join wp_postmeta pm2 on (wp_posts.id = pm2.post_id , pm2.meta_key='lat') left join wp_postmeta pm3 on (wp_posts.id = pm3.post_id , pm3.meta_key='lng') wp_posts.post_type = 'outlet' , wp_posts.post_status = 'publish' having distance < 20 order distance
Comments
Post a Comment