Support

Account

Home Forums ACF PRO Adding Sub-Field to Field Group Dynamically

Unread

Adding Sub-Field to Field Group Dynamically

  • Hi all,

    I’m working on a pretty complex ACF project where I need to dynamically add fields to a Group Field upon creation of a new product. Basically, it’s supposed to proceed as such:

    1. Admin adds a new product to a CPT named Product Info (info)
    2. Within a Feature Field Group, new product is added to a choice list for user to select (this is already working just fine) for feature to appear on.
    3. Within the same Feature Field Group, the Group field has a default option (already present) to enter meta data pertaining to default information of the feature.
    4. Field with the slug of the new product will conditionally appear if user has selected it in choice field in number 2.

    I have been attempting to dynamically add 4 to the Group field found in 3 based on running a WP Query within a function in function.php. I’ve searched exhaustingly for a similar situation but none come to mind.

    Here’s my code: `//Add New Sub Fields to ACF via PHP.
    function addGroupDynamically(){
    //Run Query to get the product info fields.
    $args = array(
    ‘numberposts’ => -1,
    ‘post_type’ => ‘info’

    );

    $products = get_posts( $args );
    //Start Dynamic Field Creation
    $the_query = new WP_Query( $args );

    while( $the_query->have_posts() ){
    $the_query->the_post();
    $productName = $product->$post_title; //Nice-name of Product. Passed to Label fields.
    $productSlug = $product->$post_name; //slug of Product. Passed to field keys

    $key = ‘group_5d77e59eb047f’;
    $value = array(
    // this is the array for the top level field “feature_selector”

    // next we have the field in the repeater
    ‘feature_description’ => array(
    ‘sub_fields’ => array(
    //Start Dynamic Field Creation
    array(
    ‘key’ => ‘field_’.$productSlug.’_tab’,
    ‘label’ => $productName,
    ‘name’ => ”,
    ‘type’ => ‘tab’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => array(
    array(
    array(
    ‘field’ => ‘field_5d77e59eb4c00’,
    ‘operator’ => ‘==’,
    ‘value’ => $productSlug,
    ),
    ),
    ),
    ‘wrapper’ => array(
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘placement’ => ‘left’,
    ‘endpoint’ => 0,
    ),
    array(
    ‘key’ => ‘field_’.$productSlug.’_content’,
    ‘label’ => $productName . ‘ Specific Content’,
    ‘name’ => $productSlug . ‘_specific_content’,
    ‘type’ => ‘group’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => array(
    array(
    array(
    ‘field’ => ‘field_5d77e59eb4c00’,
    ‘operator’ => ‘==’,
    ‘value’ => $productSlug,
    ),
    ),
    ),
    ‘wrapper’ => array(
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘layout’ => ‘block’,
    ‘sub_fields’ => array(
    array(
    ‘key’ => ‘field_’.$productSlug.’_title’,
    ‘label’ => ‘Title’,
    ‘name’ => ‘title’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array(
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ),
    array(
    ‘key’ => ‘field_’.$productSlug.’_description’,
    ‘label’ => ‘Description’,
    ‘name’ => ‘description’,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array(
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘default_value’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ‘maxlength’ => ”,
    ),
    array(
    ‘key’ => ‘field_’.$productSlug.’_default_image’,
    ‘label’ => ‘Default Image’,
    ‘name’ => ‘default_image’,
    ‘type’ => ‘image’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array(
    ‘width’ => ’50’,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘return_format’ => ‘array’,
    ‘preview_size’ => ‘thumbnail’,
    ‘library’ => ‘all’,
    ‘min_width’ => ”,
    ‘min_height’ => ”,
    ‘min_size’ => ”,
    ‘max_width’ => ”,
    ‘max_height’ => ”,
    ‘max_size’ => ”,
    ‘mime_types’ => ”,
    ),
    array(
    ‘key’ => ‘field_’.$productSlug.’_default_icon’,
    ‘label’ => ‘Default Icon’,
    ‘name’ => ‘default_icon’,
    ‘type’ => ‘image’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array(
    ‘width’ => ’50’,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘return_format’ => ”,
    ‘preview_size’ => ‘thumbnail’,
    ‘library’ => ”,
    ‘min_width’ => ”,
    ‘min_height’ => ”,
    ‘min_size’ => ”,
    ‘max_width’ => ”,
    ‘max_height’ => ”,
    ‘max_size’ => ”,
    ‘mime_types’ => ”,
    ),
    ),
    ),
    )
    )
    );
    update_field($key, $value);
    }

    }`

    Any help would be really appreciated! Thanks in advance.

Viewing 1 post (of 1 total)

The topic ‘Adding Sub-Field to Field Group Dynamically’ is closed to new replies.