Add google map locations from wordpress post type or category

Does anyone know how to add locations/markers to google maps using a wordpress query? For example, show all locations of a particular post type or category?

I can easy return a list of posts with the code below (all of which have lat/long coordinates) but can’t work out how to add it to the “var locations” in the google maps js.

$query_school = new WP_GeoQuery(array( 'latitude' =>  ''.$location_lat.'', 'longitude' => ''.$location_long.'', 'posts_per_page' => 5, 'post_type' => 'surf-school')); 

Related posts

Leave a Reply

2 comments

  1. Not sure exactly what are you trying to accomplish with lot of questions in this small piece of information. But I understand the fact that you are trying to add markers to the Google Maps and you have the location coordinates with you. The best way to do it is by using a lot of free open source wordpress plugins meant for the same purpose. I am providing the link for various plugins that would help you to add markers on Google maps when you have location coordinates with you and also you can categorize the relevant locations.

    https://www.mapsmarker.com/features/

    https://wordpress.org/plugins/codepeople-post-map/

    https://wordpress.org/plugins/wp-google-map-plugin/

    http://codecanyon.net/item/advanced-google-maps-plugin-for-wordpress/5211638

    http://www.instantshift.com/2014/01/23/wordpress-google-maps-plugins/

    http://www.elegantthemes.com/blog/tips-tricks/wordpress-google-maps-plugins

    http://www.wpmayor.com/best-google-maps-plugins-for-wordpress/

    http://www.instantshift.com/2014/01/23/wordpress-google-maps-plugins/

    You can pick any one of these..

    Hope this would Help!!

  2. Ok, I figured this out. I didn’t want to use another plugin (thanks anyway AniV) so I’m posting my solution for anyone else who needs it.

    Basically you have to loop through the query results within var locations.

    So this is the solution to add to the rest of your Google API javascript:

     var locations = [
        <?php
        $post_query = new WP_Query(array('posts_per_page' => 999, 'post_type' => array ('surfing-beach','surf-school')));
            if(!empty($post_query->posts)){
    
            foreach($post_query->posts as $post) { 
                $location_lat = get_post_meta($post->ID,'wp_gp_latitude',true);
                $location_long = get_post_meta($post->ID,'wp_gp_longitude',true);
    
            ?>
                ['<a href="<?php the_permalink();?>"><?php the_title(); ?></a>', <?php echo $location_lat; ?>, <?php echo $location_long; ?>, 1],
    
            <?php } }?>
        ];