wpgmp_query_posts_array - WP Maps Pro

15000+ live websites are using this google maps wordpress plugin.

wpgmp_query_posts_array

The wpgmp_query_posts_array hook in WP Maps Pro allows developers to modify the meta keys used to retrieve location data (such as address, latitude, longitude, category, and Advanced Custom Field key) for each post type. It is triggered when the map is generated and the plugin prepares to extract location details from the stored post meta.

Usage

Developers can implement this hook within their theme’s functions.php file or inside a custom plugin. It is particularly useful when you have stored location data using custom meta keys or ACF fields for different post types.

Example 1

This example demonstrates how to change the default meta keys for the post post type to custom field names.

add_filter('wpgmp_query_posts_array', 'custom_wpgmp_location_meta_keys', 10, 2);

function custom_wpgmp_location_meta_keys($meta_keys, $map) {
    foreach ($meta_keys as &$group) {
        if (isset($group['post'])) {
            $group['post']['address']   = '_custom_product_address';
            $group['post']['latitude']  = '_custom_product_latitude';
            $group['post']['longitude'] = '_custom_product_longitude';
            $group['post']['category']  = '_custom_product_category_id';
            $group['post']['acf_key']   = 'google_maps'; // optional ACF field custom field key
        }
    }

    return $meta_keys;
}

Example 3

To override meta keys for all post types (e.g., post, page), you can loop through the $meta_keys array and update each type:

add_filter('wpgmp_query_posts_array', 'custom_all_post_type_meta_keys', 10, 2);

function custom_all_post_type_meta_keys($meta_keys, $map) {
    foreach ($meta_keys as &$group) {
        foreach ($group as $post_type => &$fields) {
            $fields['address']   = '_my_address_key';
            $fields['latitude']  = '_my_latitude_key';
            $fields['longitude'] = '_my_longitude_key';
            $fields['category']  = '_my_category_key';
            $fields['acf_key']   = ''; // optional
        }
    }

    return $meta_keys;
}

Always ensure that the modifications you make using this hook are well-tested to prevent any unintended side effects. Proper validation and sanitization of inputs should be implemented to maintain security and performance.

Install Plugin Now!

WP MAPS PRO helps you create dynamic, customizable maps using Google Maps or Leaflet — no coding required. Includes free updates, premium support, and a 30-day money-back guarantee.