Support

Account

Home Forums ACF PRO Populate repeater from options page repeater custom fields

Helping

Populate repeater from options page repeater custom fields

  • I have an options page using ACF

    This is the structure (OPTIONS PAGE)

    - specifications - group
    
    -- specifications_repeater - repeater
    
    --- specification_name - text

    so on the options page I added 3 fields and save

    Size
    Range
    Colour

    Now I have another another custom fields set in pages

    - specifications - repeater
    
    -- specification_item - text (populate this fields based on the "specification_name" field on the options page)
    
    -- specification_value - text

    What I want and hoping that it populates whatever its in the options page, so it will populate automatically the fields ( Size, Colour & Range )

    I have this code below but it only populates “Colour” on all its fields, I think its pulling the last data.

    function my_acf_load_field($field) {
        $field['default_value'] = array();
        if( have_rows('specifications', 'option') ) {
            while( have_rows('specifications', 'option') ) {
                the_row();
    
                if( have_rows('specifications_repeater', 'option') ) {
                    while( have_rows('specifications_repeater', 'option') ) {
                        the_row();
    
                        $value = get_sub_field('specification_name');
                        $field['default_value'] = $value;
    
                    }
                }
    
            }
        }
        //$field['default_value'] = uniqid();
        return $field;
    }
    add_filter('acf/load_field/key=field_5e86e1166f98a', 'my_acf_load_field');

    also

    field_5e86e1166f98a

    is the target field

  • You can’t do what you’re attempting. The default value of a repeater sub field cannot be different for each row of a repeater.

    What you need to do is load the entire value of all rows of the repeater.

    Step 1: Set up your repeater so that it must have at least one row. This will ensure that it will have a value after someone has updated a post.

    Step 2: use an acf/load_value filter, you will need the field keys of the repeater and the sub fields.

    
    // the field key of repeater
    add_filter(acf/load_value/key=field_XXXXXXX', 'my_acf_load_field', 20, 3);
    function my_acf_load_field($value, $post_id, $field) {
      if ($value) {
        // if this field has a value then it has already be updated
        return $value;
      }
      // set up values of repeater
      if (have_rows('specifications', 'option')) {
        // initialize rows
        $rows = array();
        while (have_rows('specifications', 'option')) {
          the_row();
          // each row is a nested array using field key => value pairs
          $rows[] = array(
            // field key of the sub field
            'field_YYYYYYY' => get_sub_field('specification_name');
          );
        }
      }
      return $value;
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Populate repeater from options page repeater custom fields’ is closed to new replies.