在$wpdb查询中获取多个自定义字段值

时间:2012-10-16 作者:stefano1

Possible Duplicate:
How to query posts based on lat-lng coordinate as post meta?

我想创建一个$wpdb查询,以选择当前坐标(lat和$long)附近最近的位置(自定义post类型),但我无法同时获得两个自定义字段值(wpfc纬度和wpfc经度)。。。

如何更改查询以获得经度?

SELECT $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->terms.name, $wpdb->postmeta.meta_value AS latitude
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->postmeta.meta_key = \'wpcf-latitude\'
AND $wpdb->posts.post_status = \'publish\' 
AND $wpdb->posts.post_type = \'places\'
AND $wpdb->term_taxonomy.taxonomy = \'countries\'
更新-固定查询由于s\\u ha\\u dum,我成功地获得了这两个值,现在我可以计算距离并对结果排序。我的最后一个问题是:

SELECT $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->terms.name, wpcflat.meta_value AS latitude, wpcflong.meta_value AS longitude,
    6371 * 2 * ASIN ( SQRT (POWER(SIN(($lat - wpcflat.meta_value)*pi()/180 / 2),2) + COS($lat * pi()/180) * COS(wpcflat.meta_value *pi()/180) * POWER(SIN(($long - wpcflong.meta_value) *pi()/180 / 2), 2) ) ) as distance
FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta as wpcflong ON ($wpdb->posts.ID = wpcflong.post_id)
    LEFT JOIN $wpdb->postmeta as wpcflat ON ($wpdb->posts.ID = wpcflat.post_id)
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->posts.ID NOT IN ($post->ID)
AND wpcflat.meta_key = \'wpcf-latitude\'
AND wpcflong.meta_key = \'wpcf-longitude\'
AND $wpdb->posts.post_status = \'publish\' 
AND $wpdb->posts.post_type = \'places\'
AND $wpdb->term_taxonomy.taxonomy = \'countries\'
ORDER BY distance
LIMIT 20
要加载的查询是否繁重?你知道有没有办法优化它吗?

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

你必须在Posteta表上加入两次。类似于:

SELECT $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->terms.name, wpcflat.meta_value AS latitude, wpcflong.meta_value AS longitude
FROM $wpdb->posts
/* First Join */
LEFT JOIN $wpdb->postmeta as wpcflong ON $wpdb->posts.ID = wpcflong.post_id
/* Second Join */
LEFT JOIN $wpdb->postmeta as wpcflat ON $wpdb->posts.ID = wpcflat.post_id
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE 1
/* Meta queries for lat and long */
AND wpcflat.meta_key = \'wpcf-latitude\'
AND wpcflong.meta_key = \'wpcf-longitude\'
/* I don\'t know what criteria you are using to calculate \'nearest\' 
but you\'d need more conditions here for that */
AND $wpdb->posts.post_status = \'publish\' 
AND $wpdb->posts.post_type = \'places\'
AND $wpdb->term_taxonomy.taxonomy = \'countries\'
正如我在SQL注释中所指出的,我不知道您使用的是什么作为“最接近”的标准,但该代码将为您提供两个元数据。我的数据库没有lat和long数据,所以严格来说我无法测试,但原则上它是正确的,除非有一两个打字错误或其他愚蠢的错误。

Subqueries 是另一种可能的解决方案,但当您尝试对它们进行排序或筛选时,它们的性能并不好。

结束

相关推荐

从MySQL的类别中删除帖子

有没有办法从所有类别中删除所有帖子?我找到了这个插件,但速度很慢。我需要的是MySql查询,它将帮助我从所有类别中删除所有帖子。如果您知道如何实现相同的代码,但只针对特定的类别,那么这也将非常有用,以供参考。那么,如何将属于一个类别的所有帖子与所有类别解除关联(因此它们将只属于默认的“未分类”类别)。非常感谢。