Title: CFS synchronize for loop fields
Last modified: August 22, 2016

---

# CFS synchronize for loop fields

 *  Resolved [camiloclc](https://wordpress.org/support/users/camiloclc/)
 * (@camiloclc)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/cfs-synchronize-for-loop-fields/)
 * Before any complaints, thank you for this marvelous plugin.
 * Well, it just doesn’t work! CFS()->save always throws an error on metadata that
   belongs on a parent field.
 * As far as I can tell, the sync plugin is also developed by you, Mr. Matt Gibbs.
   So I’ll post an updated “cfs_map_values” function here:
 *     ```
       /**
        * Map postmeta to CFS fields
        */
       function cfs_map_values() {
           global $wpdb;
   
           // CFS field groups
           $field_groups = cfs_get_field_groups();
   
           $query = new WP_Query( array(
               'post_type' => 'any',
               'posts_per_page' => -1,
               'post_status' => 'publish',
               'fields' => 'ids',
           ) );
   
           foreach ( $query->posts as $post_id ) {
               $matching_groups = CFS()->api->get_matching_groups( $post_id );
               if ( ! empty( $matching_groups ) ) {
   
                   // Get all postmeta values for this post ID
                   $postmeta = get_post_meta( $post_id );
                   $field_data = array();
   
                   // See which fields are already in the cfs_values table for this post ID
                   $existing_field_ids = $wpdb->get_col( "
                       SELECT DISTINCT field_id FROM {$wpdb->prefix}cfs_values WHERE post_id IN ('$post_id')"
                   );
   
                   // Loop through field groups
                   foreach ( $matching_groups as $group_id => $group_name ) {
                       // Loop through fields within this group
                       foreach ( $field_groups[ $group_id ] as $field ) {
                           // Skip if CFS values already exist
                           if ( in_array( $field['id'], $existing_field_ids ) ) {
                               continue;
                           }
   
                           if ( ! empty( $postmeta[ $field['name'] ] ) ) {
                               $field_data[ $field['name'] ] = $postmeta[ $field['name'] ];
                           }
                       }
                   }
   
                   // Save the values
                   if ( ! empty( $field_data ) ) {
       				// Loop through field groups in search for loop fields
       				foreach ( $matching_groups as $group_id => $group_name ) {
       					// Loop through fields within this group searching for fields that have parents
       					foreach ( $field_groups[ $group_id ] as $field ) {
       						// Continue if there's no data for this field or if it doesn't have a parent
       						if ( !isset( $field_data[ $field['name'] ] ) || $field['parent_id'] == 0 ) {
       							continue;
       						}
       						// Try to find the parent field
       						$parent_field = null;
       						foreach ( $field_groups[ $group_id ] as $temp_field ) {
       							if ( $temp_field['id'] != $field['parent_id'] ) {
       								continue;
       							}
       							$parent_field = $temp_field;
       							break;
       						}
   
       						if ( is_null( $parent_field ) ) {
       							continue;
       						}
   
       						// If another field has already gone through this process, it needs to have the same number of elements
       						if ( isset( $field_data[ $parent_field['name'] ] ) && count( $field_data[ $parent_field['name'] ] ) != count( $field_data[ $field['name'] ] ) ) {
       							unset( $field_data[ $parent_field['name'] ] );
       							continue;
       						}
   
       						// Let's set the values for the parent field, and also remove "old" data
       						$values = $field_data[ $field['name'] ];
       						unset( $field_data[ $field['name'] ] );
       						if ( !isset( $field_data[ $parent_field['name'] ] ) ) {
       							$field_data[ $parent_field['name'] ] = array();
       						}
       						foreach ( $values as $i => $v ) {
       							// If no other fields that are children of this loop field have been set, initialize it's value
       							if ( !isset( $field_data[ $parent_field['name'] ][ $i ] ) ) {
       								$field_data[ $parent_field['name'] ][ $i ] = array();
       							}
       							// Sets the field value at the appropriate position
       							$field_data[ $parent_field['name'] ][ $i ][ $field['name'] ] = $v;
       						}
       					}
       				}
   
                       CFS()->save( $field_data, array( 'ID' => $post_id ) );
                   }
               }
           }
       }
       ```
   
 * All changes that were made come between:
 *     ```
       // Save the values
       if ( ! empty( $field_data ) ) {
       ```
   
 * And:
 *     ```
       CFS()->save( $field_data, array( 'ID' => $post_id ) );
       ```
   
 * The function became more expensive, but runs well. It worked perfectly for me
   on a loop field that contains only one child, but it should be more thoroughly
   tested.
 * [https://wordpress.org/plugins/custom-field-suite/](https://wordpress.org/plugins/custom-field-suite/)

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Thread Starter [camiloclc](https://wordpress.org/support/users/camiloclc/)
 * (@camiloclc)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/cfs-synchronize-for-loop-fields/#post-5562198)
 * Sorry for the mixed code formatting!
 *  Thread Starter [camiloclc](https://wordpress.org/support/users/camiloclc/)
 * (@camiloclc)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/cfs-synchronize-for-loop-fields/#post-5562201)
 * Replaced tabs with spaces
 *     ```
       /**
        * Map postmeta to CFS fields
        */
       function cfs_map_values() {
           global $wpdb;
   
           // CFS field groups
           $field_groups = cfs_get_field_groups();
   
           $query = new WP_Query( array(
               'post_type' => 'any',
               'posts_per_page' => -1,
               'post_status' => 'publish',
               'fields' => 'ids',
           ) );
   
           foreach ( $query->posts as $post_id ) {
               $matching_groups = CFS()->api->get_matching_groups( $post_id );
               if ( ! empty( $matching_groups ) ) {
   
                   // Get all postmeta values for this post ID
                   $postmeta = get_post_meta( $post_id );
                   $field_data = array();
   
                   // See which fields are already in the cfs_values table for this post ID
                   $existing_field_ids = $wpdb->get_col( "
                       SELECT DISTINCT field_id FROM {$wpdb->prefix}cfs_values WHERE post_id IN ('$post_id')"
                   );
   
                   // Loop through field groups
                   foreach ( $matching_groups as $group_id => $group_name ) {
                       // Loop through fields within this group
                       foreach ( $field_groups[ $group_id ] as $field ) {
                           // Skip if CFS values already exist
                           if ( in_array( $field['id'], $existing_field_ids ) ) {
                               continue;
                           }
   
                           if ( ! empty( $postmeta[ $field['name'] ] ) ) {
                               $field_data[ $field['name'] ] = $postmeta[ $field['name'] ];
                           }
                       }
                   }
   
                   // Save the values
                   if ( ! empty( $field_data ) ) {
                       // Loop through field groups in search for loop fields
                       foreach ( $matching_groups as $group_id => $group_name ) {
                           // Loop through fields within this group searching for fields that have parents
                           foreach ( $field_groups[ $group_id ] as $field ) {
                               // Continue if there's no data for this field or if it doesn't have a parent
                               if ( !isset( $field_data[ $field['name'] ] ) || $field['parent_id'] == 0 ) {
                                   continue;
                               }
                               // Try to find the parent field
                               $parent_field = null;
                               foreach ( $field_groups[ $group_id ] as $temp_field ) {
                                   if ( $temp_field['id'] != $field['parent_id'] ) {
                                       continue;
                                   }
                                   $parent_field = $temp_field;
                                   break;
                               }
   
                               if ( is_null( $parent_field ) ) {
                                   continue;
                               }
   
                               // If another field has already gone through this process, it needs to have the same number of elements
                               if ( isset( $field_data[ $parent_field['name'] ] ) && count( $field_data[ $parent_field['name'] ] ) != count( $field_data[ $field['name'] ] ) ) {
                                   unset( $field_data[ $parent_field['name'] ] );
                                   continue;
                               }
   
                               // Let's set the values for the parent field, and also remove "old" data
                               $values = $field_data[ $field['name'] ];
                               unset( $field_data[ $field['name'] ] );
                               if ( !isset( $field_data[ $parent_field['name'] ] ) ) {
                                   $field_data[ $parent_field['name'] ] = array();
                               }
                               foreach ( $values as $i => $v ) {
                                   // If no other fields that are children of this loop field have been set, initialize it's value
                                   if ( !isset( $field_data[ $parent_field['name'] ][ $i ] ) ) {
                                       $field_data[ $parent_field['name'] ][ $i ] = array();
                                   }
                                   // Sets the field value at the appropriate position
                                   $field_data[ $parent_field['name'] ][ $i ][ $field['name'] ] = $v;
                               }
                           }
                       }
   
                       CFS()->save( $field_data, array( 'ID' => $post_id ) );
                   }
               }
           }
       }
       ```
   
 *  Plugin Author [Matt Gibbs](https://wordpress.org/support/users/mgibbs189/)
 * (@mgibbs189)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/cfs-synchronize-for-loop-fields/#post-5562364)
 * Please submit a pull request. Thanks!
 * [https://github.com/mgibbs189/cfs-synchronize](https://github.com/mgibbs189/cfs-synchronize)
 *  Thread Starter [camiloclc](https://wordpress.org/support/users/camiloclc/)
 * (@camiloclc)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/cfs-synchronize-for-loop-fields/#post-5562437)
 * OK! Just give me a few days, I’m finishing up a project. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘CFS synchronize for loop fields’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/custom-field-suite.svg)
 * [Custom Field Suite](https://wordpress.org/plugins/custom-field-suite/)
 * [Support Threads](https://wordpress.org/support/plugin/custom-field-suite/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-field-suite/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-field-suite/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-field-suite/reviews/)

## Tags

 * [CFS](https://wordpress.org/support/topic-tag/cfs/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [synchronize](https://wordpress.org/support/topic-tag/synchronize/)

 * 4 replies
 * 2 participants
 * Last reply from: [camiloclc](https://wordpress.org/support/users/camiloclc/)
 * Last activity: [11 years, 6 months ago](https://wordpress.org/support/topic/cfs-synchronize-for-loop-fields/#post-5562437)
 * Status: resolved