Support

Account

Home Forums General Issues Getting google map field from another cpt within custom query

Solved

Getting google map field from another cpt within custom query

  • I have two custom post types: ‘shows’ and ‘venues’.

    In venues I have a google map acf field. For shows’ I have an acf relationship field to grab the venue for each show.

    My main templates use a custom wp_query of the shows cpt but within that query I want to grab the google map field from the chosen venue.

    When I just try to grab the google map field directly the map location is null.

    How can I do this?

  • Ok I figured it out:

    
    <?php
    
    $posts = get_field('venue');
     
    if( $posts ): ?>
        
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
            <?php $vmap = get_field('venue_google_map', $post->ID); ?>
            <div class="acf-map">
    	  <div class="marker" data-lat="<?php echo $vmap['lat']; ?>" data-lng="<?php echo $vmap['lng']; ?>"></div>
    	</div>
        <?php endforeach; ?>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>
    
  • Glad I found this thread! I was trying to combine a relationship field and have that place a pin on the map. Here’s the code I have that works for me:

    <?php $posts = get_field('featured_space'); if( $posts ): ?>
      <div class="acf-map">
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
          <?php setup_postdata($post); ?>
            <?php $space_address = get_field('space_address', $post->ID); ?>
              <div class="marker" data-lat="<?php echo $space_address['lat']; ?>" data-lng="<?php echo $space_address['lng']; ?>">
                <h4><?php the_title();?></h4>
                <p><?php echo $space_address['address']; ?></p>
    					</div><!--/.marker-->
    	  <?php endforeach; ?>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    	</div><!--/.acf-map-->
    <?php endif; ?>

    Or if that does not work here’s a link to a gist with the code: Gist

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Getting google map field from another cpt within custom query’ is closed to new replies.