最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Querying terms with calculations based on term meta data, sql vs. get_terms

programmeradmin1浏览0评论

Is it viable and or advisable to try and implement a query like the below through get_terms and its meta query functionality? Basically, I query terms of a specific taxonomy 'pr' that have meta fields containing coordinates based on distance from a reference coordinate. My gut sense is that it will be more ellegant and efficient to keep the little pathagoras calaculation in the db query rather than having to loop through unnecessary results in php, perhaps I'm mistaken about that? Any pointers on what the structure of the meta querie(s?) should look like?

SET @refLat = -26.080;
SET @refLng = 28.015;
SELECT
    term.term_id,
    agt_Name.meta_value AS agt_Name,
    metaLat.meta_value AS metaLat,
    metaLng.meta_value AS metaLng,
    (
        SQRT(
            POW( @refLat - metaLat.meta_value, 2 ) + POW( @refLng - metaLng.meta_value, 2 )
        )
    ) * 111139 AS metresDistance
FROM
    `wp_terms` term
INNER JOIN `wp_term_taxonomy` tax ON
    ( term.term_id = tax.term_id AND tax.taxonomy = 'pr' )
INNER JOIN `wp_termmeta` metaLat ON
    ( term.term_id = metaLat.term_id AND metaLat.meta_key = '_latitude' )
INNER JOIN `wp_termmeta` metaLng ON
    ( term.term_id = metaLng.term_id AND metaLng.meta_key = '_longitude' )
INNER JOIN `wp_termmeta` agt_Name ON
    ( term.term_id = agt_Name.term_id AND agt_Name.meta_key = 'agt_Name' )
ORDER BY
    (
        SQRT(
            POW( @refLat - metaLat.meta_value, 2 ) + POW( @refLng - metaLng.meta_value, 2 )
        )
    ) ASC
    LIMIT 0,5

Thanks in advance.

发布评论

评论列表(0)

  1. 暂无评论