Support

Account

Home Forums General Issues Add all fields for a field group via code on object creation

Solving

Add all fields for a field group via code on object creation

  • Creating or updating an object in the WP admin creates the necessary ACF rows in the object meta table (eg wp_usermeta).

    However, creating or updating the object via code (eg wp_update_user()) does not add the ACF rows, and update_field() can’t use field names if the row with the field key does not exist for that object in the object meta table.

    So, is there a way of programatically initiating the same process as happens when the Save/Update button is pressed in the backend

    Alternatively, is there a way of getting field keys for a particular field group without having to do a custom query on the post and postmeta tables?

  • Hi @digitalquery

    Thanks for the code. I did a bit of digging into the wp_update_user and it seems that ACF is not hooking into the actions which this function uses.

    I’ll add this to the to-do and make sure I update the actions to use these.

    For now, I’m not sure what the solution is. Perhaps you could modify the core code in /core/controllers/everything_fields?

    Sorry that I don’t have a great answer for you at the moment.

    Thanks
    E

  • Hey Elliot,

    Thanks for the quick reply. Quick followup.

    How does ACF hook into the back end save/update users process? Obviously, if I click “Update” on the edit user screen, ACF is creating the necessary rows.

    Alternatively, is there an easy way of getting all field keys and field names for a field group?

  • I’ve implemented this – be interested to get your opinion or is there’s a better way of doing it

    This function returns an associative array given a Field Group title. The array keys are the field name slugs, and the array values are the field keys.

    function get_acf_field_keys($field_group_title) {
    
            global $wpdb;
    
            // get the post id for this field group
            $sql = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = '%s' AND post_type = 'acf'", $field_group_title );
            $field_group_id = $wpdb->get_var($sql);
    
            // now get all postmeta items (the fields in the fieldgroup) for that postid
            $sql = $wpdb->prepare(" SELECT meta_value from $wpdb->postmeta
                                    WHERE post_id = '%d' AND meta_key LIKE 'field%%'",$field_group_id);
            $acf_postmeta_fields = $wpdb->get_results($sql);
    
            $fields = array();
            foreach ($acf_postmeta_fields as $acf_postmeta_field) {
                $unserialized = unserialize($acf_postmeta_field->meta_value);
                $fields[$unserialized['name']] =  $unserialized['key'];
            }
            return $fields;
        }
  • Hi @digitalquery

    If you take a look in the everything_fields.php file, you will see the actions that ACF uses to hook into the user saving process.

    Currently, there is no easy way of getting all field keys and field names for a field group. This is one of the main reasons as to why I have been working on v5 with a public back end API.

    Thanks
    E

  • It seems the problem remains. I have to massively import users but wp_update_user() or update_user_meta() don’t create field_id.
    And, apparently, there’s no ACF alternative (if you want to update a field’s value, you must have the field_id).
    How can we do ?
    Thanks.

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

The topic ‘Add all fields for a field group via code on object creation’ is closed to new replies.