I have a google map in my wordpress site, this map collects markers that are created from posts. To explain a little better, a user add’s an item, that item is then placed on a map based on location and then a total inventory needs to be totaled. Each location should only generate one marker and the total inventory of that marker’s product. I have a few select dropdown’s that let the use filter through the markers, and on change, updates an ajaxed php file, which builds a json file of the results, which the maps javascript then loops through and displays.
I have everything displaying, but, I can’t get the markers to group by location and the total inventory to display. I think the ultimate solution would be to have the php script that generates the json file create one marker per location with the totals, eliminating the need for filtering with javascript. Here is my PHP that creates the JSON file.
<?php
//Header
include "../../../../wp-blog-header.php";
global $wpdb;
//$wpdb->show_errors();
$eq_type = $_GET['eq_type'];
$sql = " SELECT wp_posts.ID, wp_postmeta.meta_value, wp_postmeta.meta_key
FROM wp_posts
LEFT JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_posts.post_type = 'equipment'
AND wp_posts.post_status = 'publish'
AND wp_postmeta.meta_key NOT LIKE '%_yoast_wpseo%'
AND wp_postmeta.meta_key NOT LIKE '%_edit%'
";
$posts = $wpdb->get_results($sql);
$arr = array();
foreach ($posts as $post):
$arr["item-".$post->ID][$post->meta_key] = $post->meta_value;
//print $post->ID.' - '.$post->meta_key." - ".$post->meta_value."<br />";
endforeach;
$fp = fopen('../json/json-equipment.json', 'w');
fwrite($fp, json_encode($arr));
fclose($fp);
?>
And it generates JSON output like this…
{
"item-111":
{"eq_type":"Dump Truck","eq_region":"Alberta","eq_total_inventory":"123"},
"item-112":
{"eq_type":"Dump Truck","eq_region":"British Columbia","eq_total_inventory":"234"},
"item-113":
{"eq_type":"Dump Truck","eq_region":"British Columbia","eq_total_inventory":"23"},
"item-114":
{"eq_type":"Digger","eq_region":"Manitoba","eq_total_inventory":"23"}
}
So hopefully i’ve been clear enough, need to generate only 1 marker per location, with the total inventory of each location add’ed up that filters by Type and/or Region… but the grouping and totalling is what is really stumping me. Ask for any information!