Support

Account

Home Forums Backend Issues (wp-admin) [q] Is it possible to add fields automatically after adding in other group?

Solved

[q] Is it possible to add fields automatically after adding in other group?

  • [non pro version]
    Problem:
    There are fields in group existing:
    A
    B
    C
    For each field created I need something like Ax and Ay in other field group.
    Is it possible to make this process automatic?
    I mean, when I add some field to one fields group (D) and for example some filter usage would add new fields (Dx and Dy)into other group.

  • Hi @razider

    I believe you can use the acf/update_field_group and acf_update_field() function to do that. Here’s a simple example you can try:

    add_action('acf/update_field_group', 'my_acf_add_custom_field');
    function my_acf_add_custom_field($field_group){
        
        // set ID of field groups you want to update
        $field_group_ids = array(4002);
        
        // loop through the IDs
        foreach( $field_group_ids as $field_group_id ){
            
            // set the new field data
            $field = array(
                'key' => 'field_1',
                'label' => 'Sub Title',
                'name' => 'sub_title',
                'type' => 'text',
                'parent' => $field_group_id
            );
            
            // add the field
            $field_id = acf_update_field($field);
            
        }
        
    }

    I hope this helps 🙂

  • I will check this tommorow, but I’m pretty sure it will work. Thank you (thank you thank you thank you) very much.

    One more question.

    This $field array, where can I find how (for example)integer field is built?
    I mean those labels, types etc.

  • Hi @razider

    You can check the field data by using the field group ID like this:

    echo "<pre>";
    
    // get the field group data by using the ID
    $fields = acf_get_fields(99);
    
    // loop and print the fields data
    foreach( $fields as $field ){
        
        // print it
        print_r($field);
        
    }
    
    echo "</pre>";

    Where “99” is the field group ID.

    I hope this helps 🙂

  • Meh. Thats logical. 😀 I should came up with this solution.

    Now I should be fine. Thank you for support don James!

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

The topic ‘[q] Is it possible to add fields automatically after adding in other group?’ is closed to new replies.