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.
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
The topic ‘CFS synchronize for loop fields’ is closed to new replies.