Been hitting my head against a wall for a couple weeks... I'm working on modifying a geotagging Wordpress plugin called Thesography. I have notified the developer of my past enhancements, and they are OK with it because this plugin is licensed under the terms of the GNU General Public License.

Currently the plugin will return a mini popup Google map for a single post, with a single image. I am looking to expand upon it to have another function which will display 1 large Google Map. This larger map will have all my geotagged images as pinpoints Then, using the Google Maps API, also display a thumbnail and post title for the specific image and pinpoint.

Onto the code:

Here is a snippet of the current code from the plugin. This snippet which retrieves the image data from the database for the specific image. As you can see, this code works for the current post. How can it be modified to include all images from all posts?

    if (is_null($imgID)) {
    $images = get_children(array(
        'post_parent' => $post->ID,
        'post_type' => 'attachment',
        'numberposts' => 1,
        'post_mime_type' => 'image',
        'orderby' => 'ID',
        'order' => 'ASC'
    if ($images) {
        foreach ($images as $image) {
            $imgID = $image->ID;

$imgmeta = wp_get_attachment_metadata($imgID);

if ($imgmeta)
if ($imgmeta['image_meta']['latitude'])
    $latitude = $imgmeta['image_meta']['latitude'];
if ($imgmeta['image_meta']['longitude'])
        $longitude = $imgmeta['image_meta']['longitude'];
if ($imgmeta['image_meta']['latitude_ref'])
     $lat_ref = $imgmeta['image_meta']['latitude_ref'];
if ($imgmeta['image_meta']['longitude_ref'])
        $lng_ref = $imgmeta['image_meta']['longitude_ref'];

$lat = geo_single_fracs2dec($latitude);
$lng = geo_single_fracs2dec($longitude);
if ($lat_ref == 'S') { $neg_lat = '-'; } else { $neg_lat = ''; }
if ($lng_ref == 'W') { $neg_lng = '-'; } else { $neg_lng = ''; }

$latlng = $neg_lat . number_format($lat,6) . ',' . $neg_lng . number_format($lng, 6);

And here's a sample snippet of the Google Maps API that I need to fit this latitude, longitude into. It will contain the latitude, longitude, the image source URL and the title of the post it displays in. That way someone can hover over a pinpoint, see the image, then click it to read more.

  var point = new GLatLng(43.91892,-78.89231);
  var marker = createMarker(point,'<a href="http://#>The Title of the Page</a><br><img src="http://image.jpg" width=150 height=100>')

It's a fun one! I have been hitting my head against the wall and have come up blank each time I try.

1 Answer 1


Get all Images:

$all_images = get_posts( array(
     'post_type' => 'attachment'
    ,'numberposts' => -1
) );

foreach ( $all_images as $img )
    echo $img;

Some notes:

  • You can use extract to make your code shorter:

    extract( $image['image_meta'], EXTR_SKIP );
    echo $latitude;
  • Use Yoda Conditions.
  • When writing php on one line, you can also use shorthand php:

    // Instead of 
    if ($lng_ref == 'W') { $neg_lng = '-'; } else { $neg_lng = ''; }
    // Write Shorthand
    $neg_lng = 'W' === lng_ref ? '-' : '';
  • And stay to STRICT type checks:

    // Instead of just checking if the value is met,...
    if ( 'a' == $b )
    // ...also check if the value has the same type:
    if ( 'a' === $b )
  • 1
    This is helpful, but I'm still struggling. When I put in your "Get All Images" code, I am given an error in the logs: PHP Catchable fatal error: Object of class stdClass could not be converted to string. So I used a var_dump() on $all_images, and quite a bit of information is returned. More than I expected. Is there a way to filter that information down? Or is that the job of the extract code?
    – Pat
    Feb 29, 2012 at 14:05
  • The error is normal, as I echoed the object. You'll have to access the parts of your object (the ones you need) with echo $img->ID; for example. Just take a look at your var_dump for what you can use.
    – kaiser
    Feb 29, 2012 at 14:16
  • Got it. Last question (I think) Now that we have a list of IDs, can you share the code that would extract the lat/long out of the metadata for each ID? I put in the extract snippet you provided, but it didn't return anything. Perhaps it's my newbieness shining through on this one.
    – Pat
    Feb 29, 2012 at 15:31
  • Just loop through $all_images and build in your foreach()-loop something like $all_w_meta[] = wp_get_attachment_metadata( $img->ID );. Then access the new array and do whatever you need - see extract() for ex. Anyway that's plain php, so you might be better off consulting php.net function reference (linked ↑).
    – kaiser
    Feb 29, 2012 at 15:44
  • Is it also possible to find the post that the image ID is attached to using this foreach() loop?
    – Pat
    Feb 29, 2012 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.