AdvancedCustomFields / acf

Advanced Custom Fields
http://advancedcustomfields.com/
871 stars 182 forks source link

Groups are duplicated automatically and somes groups are converteds to posts. #400

Open souricardomaia opened 4 years ago

souricardomaia commented 4 years ago

Hey everyone,

Did anyone have problems with automatic duplication of groups? I noticed that some groups started to appear as posts. Did this happen to any of you?

I`m using a external script (by @jessepearson) to sync modified groups automatically. Do you think it can be causing this error?

function lgr_sync_acf_fields() {

                    // vars
                    $groups = acf_get_field_groups();
                    $sync   = array();

                    // bail early if no field groups
                    if( empty( $groups ) )
                        return;

                    // find JSON field groups which have not yet been imported
                    foreach( $groups as $group ) {

                        // vars
                        $local      = acf_maybe_get( $group, 'local', false );
                        $modified   = acf_maybe_get( $group, 'modified', 0 );
                        $private    = acf_maybe_get( $group, 'private', false );

                        // ignore DB / PHP / private field groups
                        if( $local !== 'json' || $private ) {

                            // do nothing

                        } elseif( ! $group[ 'ID' ] ) {

                            $sync[ $group[ 'key' ] ] = $group;

                        } elseif( $modified && $modified > get_post_modified_time( 'U', true, $group[ 'ID' ], true ) ) {

                            $sync[ $group[ 'key' ] ]  = $group;
                        }
                    }

                    // bail if no sync needed
                    if( empty( $sync ) )
                        return;

                    if( ! empty( $sync ) ) { //if( ! empty( $keys ) ) {

                        // vars
                        $new_ids = array();

                        foreach( $sync as $key => $v ) { //foreach( $keys as $key ) {

                            // append fields
                            if( acf_have_local_fields( $key ) ) {

                                $sync[ $key ][ 'fields' ] = acf_get_fields( $key );

                            }
                            // import
                            $field_group = acf_import_field_group( $sync[ $key ] );
                        }
                    }
                }
                add_action( 'admin_init', 'lgr_sync_acf_fields' );

elliotcondon commented 4 years ago

Hi @rmaaia

I can't see anything obvious in the code snippet that would cause Field Groups to become the "post" post type. The logic here looks to be plucked from a previous version of the ACF codebase, but set to run on each admin page load.

I would suggest that you setup some tests, and get to work debugging the code!