The wpgmp_query_posts_array hook in WP Maps Pro allows developers to customize the query used to fetch posts that have a location assigned through custom fields. It is triggered when the map is generated and needs to query posts with location data.
Usage
Developers can implement this hook within their theme’s functions.php
file or within a custom plugin. This hook is particularly useful for modifying the query to include additional criteria or to alter the default behavior of how posts with location data are fetched. To use this hook effectively, understand the data structure and parameters it accepts.
Example 1
This example demonstrates how to modify the query to only include posts from a specific category.
add_filter('wpgmp_query_posts_array', 'modify_posts_query_by_category', 10, 2); function modify_posts_query_by_category($filter_array, $map) { if(isset($filter_array['post'])) { $filter_array['post']['category_name'] = 'specific-category'; } return $filter_array; }
Example 2
In this example, the query is adjusted to fetch posts within a certain date range.
add_filter('wpgmp_query_posts_array', 'filter_posts_by_date_range', 10, 2); function filter_posts_by_date_range($filter_array, $map) { if(isset($filter_array['post'])) { $filter_array['post']['date_query'] = array( array( 'after' => 'January 1st, 2023', 'before' => 'December 31st, 2023', 'inclusive' => true, ), ); } return $filter_array; }
Example 3
This example shows how to modify the query to include posts that are tagged with a specific custom field value.
add_filter('wpgmp_query_posts_array', 'filter_posts_by_custom_field', 10, 2); function filter_posts_by_custom_field($filter_array, $map) { if(isset($filter_array['post'])) { $filter_array['post']['meta_query'] = array( array( 'key' => '_custom_field_key', 'value' => 'desired_value', 'compare' => '=', ), ); } return $filter_array; }
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.