Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter camiloclc

    (@camiloclc)

    OK! Just give me a few days, I’m finishing up a project. Thanks!

    Thread Starter camiloclc

    (@camiloclc)

    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 ) );
                }
            }
        }
    }
    Thread Starter camiloclc

    (@camiloclc)

    Sorry for the mixed code formatting!

    Thread Starter camiloclc

    (@camiloclc)

    Sorry, I think I got it all wrong. This probably happened because I had WordPress SEO’s feature of removing the category base turned on. WP_DEBUG was enabled because I’m on a local machine.
    But, either way, I recommend setting $taxonomy to a default invalid value, like false, and testing for it before progressing through the function.
    Again, I’m sorry for wasting your time.

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