Support

Account

Home Forums General Issues Update fields after field group is set

Solving

Update fields after field group is set

  • What I am doing is looping through rows of data and trying to assign fields to a field group based on those results. The issue I am facing is that it seems acf_add_local_field_group() only fires when hooked early on to an action like init, so it loads an empty array of fields and adding fields further in the execution is not working like I’d hoped.

    Here is a basic example of what I am trying to do:

    acf_add_local_field_group(array(
            'key' => 'product_data',
            'title' => 'Product Data',
            'fields' => array(),
            'location' => array(
                array(
                    array(
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'product',
                    ),
                ),
            ),
        ));
    
                   foreach( $row as $r ){
                        $field_key = preg_replace('/\s*/', '', $r);
                        $field_key = strtolower($field_key);
                        acf_add_local_field(array(
                            'key' => $field_key,
                            'label' => $r,
                            'name' => $field_key,
                            'type' => 'text',
                            'parent' => 'product_data'
                        ));
                    }

    I have confirmed that within the loop, $r is a returning a proper label and $field_key is that same label with whitespace removed and set to all lowercase. In this example, when they are called from the same file (currently testing in a theme template file for convenience but will end up in a plugin) no field group is even added. If I move the field group function to functions.php and hook to init, then it will create the field group with no fields. Is it possible for me to add fields to the field group AFTER it has been initialized with an empty array?

    If it helps any, my end goal is to have a custom importer that takes column headers from a spreadsheet, creates a field for each header, then create new products for each row in the spreadsheet, adding the row data into the newly created fields.

  • Group key must start with group_ https://www.advancedcustomfields.com/resources/register-fields-via-php/#group-settings. Not using this will have some of the effects that you mention. The field group may or may not be be created and trying to use the invalid group key for the parent of the fields is also an issue. Not sure about your field keys, they must start with field_ https://www.advancedcustomfields.com/resources/register-fields-via-php/#field-settings

  • Oh wow, can’t believe I looked over that. For some reason I was under the impression that they just had to be unique strings, I wasn’t aware the group_ and field_ were actually required prefixes. I am going to make these updates and test again. Thanks a lot John!

  • Hey John,

    I made some adjustments based on your reply but the fields are still not populating. The field group is created with no fields, if you happen to have any other ideas that I could try.

    In functions.php – This seems fine and is registering the group for the post type.

    function test_update_acf_field_group() {
        acf_add_local_field_group(array(
            'key' => 'group_5',
            'title' => 'Product Data',
            'fields' => array(),
            'location' => array(
                array(
                    array(
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'product',
                    ),
                ),
            ),
        ));
    }
    add_action('init', 'test_update_acf_field_group');

    This part is in a template file that I have assigned to a page and refresh to execute the code quickly for testing (which is initially why I thought maybe the template is loading at the wrong time to add the fields). I have echoed out the variables and copy/pasted the responses as comments so you can see the actual values coming out in the first run through that loop.

    $count = 1;
    foreach( $row as $r ){
        echo $count;  // shows 1
        echo $r;      // shows Part_Number
    
        acf_add_local_field(array(
            'key' => 'field_' . $count,
            'label' => $r,
            'name' => $r,
            'type' => 'text',
            'parent' => 'group_5'
        ));
    
        $count++;
        break;
    }

    Thanks again for your help!

  • The template file that you have your code in will not be loaded in the admin, so nothing in it will run and fields will not be added. Unless I’m misunderstanding something, you need to move this to your functions.php file and add the fields after you create the group.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.