Replace/override your map query settings to showcase diverse locations on any page


In this tutorial, you will learn how to replace/override your existing map query settings with customized ones to showcase different posts or locations on a specific page.

  1. Create a new map #.
  2. Use the PHP code to insert your map inside your taxonomy page #. (e.g. echo do_shortcode('[cspm_main_map id="65"]'); ).
  3. Copy/Paste the following code in the file “functions.php” of your theme/child theme:
/**
 * Override the existing map query settings and display locations based on new custom query args.
 */
function cspm_custom_map_query_settings($map_settings, $map_id, $fields_prefix){

	if(!class_exists('CspmMainMap'))
        return; 
	
	// Specify the page ID and map ID where you want to apply these custom settings
	if(is_page(10) && $map_id == '65'){ // Change the page ID & the map ID  as needed
        
        /**
         * Status
         * ====== */
        
        	$map_settings[$fields_prefix.'_items_status'] = maybe_serialize(array('publish', 'draft')); // Must be SERIALIZED! 
        
        /**
         * Taxonomies
         * ========== */
        
            $taxonomies = array(
                array(
                    'slug' => 'category', // Taxonomy slug
                    'terms_ids' => array(1,2,3), // Taxonomy term(s) ID(s).
                    'operator' => 'IN', // Possible values are 'IN', 'NOT IN', 'AND', 'EXISTS' and 'NOT EXISTS'.
                ),
                array(
                    'slug' => 'post_tag',
                    'terms_ids' => array(1,2),
                    'operator' => 'IN',
                ),
                // Add more arrays for additional taxonomies
            );
        
            foreach($taxonomies as $taxonomy){
                $slug = $taxonomy['slug'];
                $terms_ids = $taxonomy['terms_ids'];
                $operator = $taxonomy['operator'];
                $map_settings[$fields_prefix.'_taxonomie_'.$slug] = maybe_serialize($terms_ids); // Must be SERIALIZED!
                $map_settings[$fields_prefix . '_'.$slug.'_operator_param'] = $operator;
            }

            if(count($taxonomies) > 1){
                // The logical relationship between each inner taxonomy array when there is more than one. 
                // Possible values are 'AND', 'OR'.
                $map_settings[$fields_prefix.'_taxonomy_relation_param'] = 'OR'; 
            }
        
        /**
         * Custom fields
         * ============= */
        
            $custom_fields = array(
                array(
                    'custom_field_name' => 'YOUR_CUSTOM_FIELD_KEY', // Custom field key/name
                    'custom_field_values' => 'YOUR_CUSTOM_FIELD_VALUE', // Custom field value. Separate multiple values by comma!
                    'custom_field_type' => 'CHAR', // Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
                    'custom_field_compare_param' => '=', // Operator to test the 'meta_value'. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'NOT EXISTS', 'REGEXP', 'NOT REGEXP' or 'RLIKE'. 
                ), 
                array( 'custom_field_name' => 'YOUR_CUSTOM_FIELD_KEY',
                    'custom_field_values' => 'YOUR_CUSTOM_FIELD_VALUE',
                    'custom_field_type' => 'CHAR',
                    'custom_field_compare_param' => 'LIKE',
                ),
                // Add more arrays for additional custom fields
            );
        
            if(count($custom_fields) > 0){
                $map_settings[$fields_prefix.'_custom_fields'] = maybe_serialize($custom_fields);
            }

            if(count($custom_fields) > 1){
                // The logical relationship between each inner meta_query array when there is more than one.
                // Possible values are 'AND', 'OR'.
                $map_settings[$fields_prefix.'_custom_field_relation_param'] = 'OR'; 
            }
            
        /**
         * Posts in
         * ======== */
        
            $post_in = array(1,2,3); // Use post ids. Specify posts to retrieve.

            if(is_array($post_in) && count($post_in) > 0){
                $map_settings[$fields_prefix.'_post_in'] = maybe_serialize($post_in); // Must be SERIALIZED!
            }
            
        /**
         * Posts not in 
         * ============ */
        
            $post_not_in = array(1,2,3); // Use post ids. Specify posts NOT to retrieve.

            if(is_array($post_not_in) && count($post_not_in) > 0){
                $map_settings[$fields_prefix.'_post_not_in'] = maybe_serialize($post_not_in); // Must be SERIALIZED!
            }

        /**
         * Order & Order by
         * ================ */
        
            $map_settings[$fields_prefix.'_orderby_param'] = 'ID'; // Sort retrieved posts by parameter. More details at https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
            $map_settings[$fields_prefix.'_order_param'] = 'DESC'; // Designates the ascending or descending order. Possible value are "ASC" or "DESC".
        
	}
	    
	return $map_settings;
	
}
add_filter('cspm_map_settings', 'cspm_custom_map_query_settings', 10, 3);

Here’s how you can adapt this code to your needs:

  1. Specify the page ID and map ID where you want to apply these custom settings by changing the conditions inside the if statement.
  2. Modify the settings inside the if statement, such as status, taxonomies, custom fields, posts in, posts not in, order, and order by parameters, according to your requirements.
  3. Add or remove taxonomies, custom fields, post IDs, or adjust the order and order by parameters as needed.

Ensure to customize the settings carefully according to your specific use case and requirements. To access comprehensive details regarding WordPress query settings, follow this link.


In the same context