使用$wpdb获取帖子特色图像ID

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

我不是一个sql专家,但在一些帮助下,并从我在web上找到的查询中复制,我得到了一个有效的查询,它可以获得特定纬度和经度(lat和long)最近的20个位置(自定义帖子类型)。如何编辑此查询,使其也包含帖子缩略图ID?我想这样做是为了减少页面中的查询数量,所以我认为直接使用此查询来查找值,而不是每次使用get\\u post\\u thumbnail\\u id(),我会保存一些,对吗?

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网友:Dogbert 整理而成

尚未对其进行测试,但这应该可以:

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, wpcfthumbnail AS thumbnail_id;
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)
    LEFT JOIN $wpdb->postmeta AS wpcfthumbnail ON ($wpdb->posts.ID = wpcfthumbnail.post_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\'
AND wpcfthumbnail.meta_key = \'_thumbnail_id\'
ORDER BY DISTANCE
LIMIT 20
我添加了以下代码:

, wpcfthumbnail AS thumbnail_id;
...
LEFT JOIN $wpdb->postmeta AS wpcfthumbnail ON ($wpdb->posts.ID = wpcfthumbnail.post_id)
...
AND wpcfthumbnail.meta_key = \'_thumbnail_id\'
编辑:这很有效。

SELECT $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->terms.name, getthumbinfo.meta_value AS metainfo,
    thelat.meta_value AS latitude,thelong.meta_value AS longitude, 
    ASIN ( SQRT (POWER(SIN(($lat - thelat.meta_value)*pi()/180 / 2),2) + COS($lat * pi()/180) * COS(thelat.meta_value *pi()/180) * POWER(SIN(($long - thelong.meta_value) *pi()/180 / 2), 2) ) ) AS distance
FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta AS thelong ON ($wpdb->posts.ID = thelong.post_id)
    LEFT JOIN $wpdb->postmeta AS thelat ON ($wpdb->posts.ID = thelat.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)
    LEFT JOIN $wpdb->postmeta AS getthumbid ON ($wpdb->posts.ID = getthumbid.post_id)
    LEFT JOIN $wpdb->postmeta AS getthumbinfo ON (getthumbid.meta_value = getthumbinfo.post_id)
WHERE $wpdb->posts.ID NOT IN ($post->ID)
    AND thelat.meta_key = \'wpcf-latitude\'
    AND thelong.meta_key = \'wpcf-longitude\'
    AND $wpdb->posts.post_status = \'publish\' 
    AND $wpdb->posts.post_type = \'places\'
    AND $wpdb->term_taxonomy.taxonomy = \'countries\'
    AND getthumbid.meta_key = \'_thumbnail_id\'
    AND getthumbinfo.meta_key = \'_wp_attachment_metadata\'
ORDER BY distance
LIMIT 12

结束