Title: Google map posts using meta values
Last modified: August 30, 2016

---

# Google map posts using meta values

 *  [btees](https://wordpress.org/support/users/btees/)
 * (@btees)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/google-map-posts-using-meta-values/)
 * I’m trying to create a map that will show posts based on the meta values for 
   lat and long.
 * I’m running into a wall because both latitude and longitude are meta keys and
   the code below is only outputting one meta value per entry. So it’s creating 
   two entries per post, one with lat and one with long.
 * Any advice would be greatly appreciated
 *     ```
       <?php
       $lat = 'latitude';
       $long = 'longitude';
           $results = $wpdb->get_results( "
               SELECT
                   post_title,
               meta_value
               FROM {$wpdb->posts}
                   INNER JOIN {$wpdb->postmeta}
               ON ( $wpdb->posts.ID = {$wpdb->postmeta}.post_id )
               WHERE meta_key = '$lat'
               OR meta_key = '$long'
           " );
   
           foreach( $results as $result )
           {
               printf( '<pre>%s</pre>', htmlspecialchars( var_export( $result, true ) ) );
           }
   
       ?>
   
       <style type="text/css">
             html, body { height: 100%; margin: 0; padding: 0; }
             #map { height: 350px; width:100%; }
           </style>
   
         <script type="text/javascript">
   
       // MAP
   
       function initMap() {
         var map = new google.maps.Map(document.getElementById('map'), {
           zoom: 11,
           center: {lat: 53.343156, lng: -6.258545},
           scrollwheel: false
         });
   
         setMarkers(map);
       }
   
       // LOCATIONS
   
       var locations = [
       <?php
   
           foreach( $results as $result ) {
   
               echo "\t\t\t['"
                    . str_replace("'", "\'", $result[0])
                    . "', " . $result[1]
                    . ", '" . $result[3] . "'"
                    . "],\n";
           } ?>            ];
   
       function setMarkers(map) {
         // Adds markers to the map.
   
         for (var i = 0; i < results.length; i++) {
           var result = results[i];
           var marker = new google.maps.Marker({
             position: {lat: result[1], lng: result[2]},
             map: map,
             title: result[0]
           });
         }
       }
           </script>
           <script async defer src="https://maps.googleapis.com/maps/api/js?key=MyKey&callback=initMap">
           </script>
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [btees](https://wordpress.org/support/users/btees/)
 * (@btees)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/google-map-posts-using-meta-values/#post-6466647)
 * Oh, and the printf is just for testing outputs

Viewing 1 replies (of 1 total)

The topic ‘Google map posts using meta values’ is closed to new replies.

## Tags

 * [get_results](https://wordpress.org/support/topic-tag/get_results/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [wpdb](https://wordpress.org/support/topic-tag/wpdb/)

 * In: [Everything else WordPress](https://wordpress.org/support/forum/miscellaneous/)
 * 1 reply
 * 1 participant
 * Last reply from: [btees](https://wordpress.org/support/users/btees/)
 * Last activity: [10 years, 9 months ago](https://wordpress.org/support/topic/google-map-posts-using-meta-values/#post-6466647)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
