Advanced Custom Fields and Google Maps Multiple Markers

I have some code that generates a Google Map to show a marker based on the location field supplied via a custom field named ‘event_map’. It works great.

<?php 
$location = get_field('event_map');
if( !empty($location) ):
?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
</div>
<?php endif; ?>

I now want to add a map that shows ALL of the markers from ALL of the posts from the ‘tour-dates’ custom post type. How would I go about doing this assuming all of these posts have a field called ‘event_map’ to draw location data from? I am stumped and any help appreciated.

Related posts

Leave a Reply

2 comments

  1. Get the posts with such post type and loop on it then use the ACF API get_field (the second parameter let us define the post ID in which your custom field value belongs to).

    <?php 
    $tours = get_posts( array(
       'post_type' => 'tour-dates', 
       'posts_per_page' => -1
    ));
    
    if( !empty($tours) ): ?>
    
    
    <div class="acf-map">
    
      <?php foreach($tours as $tour): ?>
        <?php
         $location = get_field('event_map',$tour->ID);
    
         if( !empty($location) ): ?>
    
         <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
    
        <?php endif; ?>
      <?php endforeach; ?> 
    
    </div> 
    
    <?php endif; ?>
    
  2. This query should get all tour-dates posts that have information in the event_map field.

    $new_loop = new WP_Query( array(
        'post_type' => 'tour-dates',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'event_map',
                'compare' => 'EXISTS'
            )
        )
    ) );
    

    I’m not sure what you’re using to display the map, so I can’t help beyond that.