Support

Account

Home Forums ACF PRO Is it safe to use the same field name in different field groups Reply To: Is it safe to use the same field name in different field groups

  • @acf-support forgive me for resuming an old post, but it would seem the fields get confused script-wise if they have the same name.

    I encountered the need to copy all ACF fields from a group_a of a post type type_a to a group_b of a post type type_b due to an analysis mistake.

    So, let’s consider this code snippet

    
            if ($tecnologie = get_field('tecnologie', $post->ID)) {
                $tecnologie_new = '<ul>';
                foreach ($tecnologie as $tecnologia) {
                    $tecnologie_new .= '<li>';
                    $tecnologie_new .= $tecnologia['tecnologia'];
                    $tecnologie_new .= '</li>';
                }
                $tecnologie_new .= '</ul>';
                update_field('tecnologie', $tecnologie_new, $new_id);
            }
    

    Here, $post is of type type_a and $new_id is the ID of a new post I created of type type_b. In both field groups there’s a field called tecnologie, which is a Repeater in the first type and a WYSIWYG Editor in the second. I basically need to turn the repeated rows into a <ul> tag.

    Post type_a field group, field tecnologie:

    Post type_b field group, field tecnologie:

    The above script returns an error:
    ( ! ) Warning: Invalid argument supplied for foreach() in (…)\wp-content\plugins\advanced-custom-fields-pro\pro\fields\class-acf-field-repeater.php on line 844

    Because the type of field the plugin considers is the Repeater, despite the required update is on the WYSIWYG.