Erhalten Sie eine Bild-ID mit $ wpdb

Ich bin kein Sql-Experte, aber mit etwas Hilfe und Kopieren von Abfragen, die ich im Internet gefunden habe, habe ich diese Arbeit, die die 20 nächstgelegenen Orte (benutzerdefinierte Post-Typ) zu einem bestimmten Breitengrad und Längengrad ($ Lat und $ lange). Wie kann ich diese Abfrage so bearbeiten, dass sie auch die Post-Miniaturansicht enthält? Ich möchte dies tun, um die Anzahl der Abfragen in meinen Seiten zu reduzieren, so denke ich, dass die Suche nach dem Wert direkt mit dieser Abfrage anstelle von get_post_thumbnail_id () jedes Mal, wenn ich einige speichern werde, oder?

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 

Vielen Dank im Voraus an alle!

Solutions Collecting From Web of "Erhalten Sie eine Bild-ID mit $ wpdb"

Habe es nicht getestet, aber das sollte funktionieren:

 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 

Ich habe diese Code-Teile hinzugefügt:

 , wpcfthumbnail AS thumbnail_id; ... LEFT JOIN $wpdb->postmeta AS wpcfthumbnail ON ($wpdb->posts.ID = wpcfthumbnail.post_id) ... AND wpcfthumbnail.meta_key = '_thumbnail_id' 

Edit: Das funktioniert.

 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