Support

Account

Home Forums ACF PRO Multiple queries single page (map markers)

Solved

Multiple queries single page (map markers)

  • Hi, trying to create a google map with multiple markers from POSTS. It’s working, after much tearing out of hair – with exception of the markers. The query is pulling through multiple instances of the same location… e.g.

    	<div class="acf-map">
    
    			
    		<div class="marker" data-lat="54.5650745979794" data-lng="-6.012410807519473"></div>
    		
    			
    		<div class="marker" data-lat="54.5650745979794" data-lng="-6.012410807519473"></div>
    		
    	
    		
    	</div>

    There’s a loop of posts before that which is working fine:

    		<?php
    			$args = array( 'post_type' => 'members', 'posts_per_page' => -1 );
    			$loop = new WP_Query( $args ); 
    			
    			while ( $loop->have_posts() ) : $loop->the_post(); ?>
    		
    		<div class="col-md-4 col-sm-6">
    
    		   <div class="member"><!-- member -->
    		   	<img src="<?php echo the_field('image'); ?>" alt="temp" class="img-thumbnail center-block" />
    			   <h3><?php echo the_field('company'); ?></h3>
    			   <?php echo the_field('proprietor'); ?>
    
    			   <h3>Services</h3>
    
    			   <?php
    				   $services = get_field('services');
    				   if( $services ): ?>
    				<ul>
    					<?php foreach( $services as $service ): ?>
    						<li><?php echo $service; ?></li>
    					<?php endforeach; ?>
    				</ul>
    				<?php endif; ?>
    
    			   <a href="<?php the_permalink(); ?>" class="btn btn-primary">More Info</a>
    	   
    
    			</div><!-- .member -->
    
    	   </div>
    		
    		<?php endwhile; ?>

    Then the repeat further down the page is the map:

    	<?php $location = get_field('location'); ?>
    
    	<div class="acf-map">
    
    	<?php 
    
    		$args = array( 'post_type' => 'members', 'posts_per_page' => -1 );
    			$loop = new WP_Query( $args ); 
    			
    			while ( $loop->have_posts() ) : $loop->the_post(); ?>
    		
    		<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
    		
    	<?php endwhile; ?>

    Can anyone help, why it won’t loop at the second query? I’ve tried resetting the query but it is global so doesn’t work.

  • If I run the location parameter directly, it works showing each value as expected, but I still can’t get it to pull out the LAT & LONG values! How do I pull these from the object?

    <div class="map">
    	<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    		<div class="marker"><?php echo the_field('location'); ?></div>
    	<?php endwhile; ?>
    </div>
  • Hi @rickblackdog

    I believe that is because you were trying to get the location outside the loop. Could you please move it into the loop like this:

    <?php 
    
    $args = array( 'post_type' => 'members', 'posts_per_page' => -1 );
    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
        <?php $location = get_field('location'); ?>
        <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
        
    <?php endwhile; ?>

    I hope this helps 🙂

  • Praise the Lord – and also, James! Thank you for your help.

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

The topic ‘Multiple queries single page (map markers)’ is closed to new replies.