Support

Account

Home Forums Add-ons Repeater Field Create New Repeater Subfield row through code

Solved

Create New Repeater Subfield row through code

  • In reference to the following link: http://support.advancedcustomfields.com/forums/topic/create-new-repeater-row-from-front-end-form/

    The code in the post above does not actually work. The output would come out quite wonky, see below. What I’m trying to do is create new rows programmatically when a new post is created, but the lack of a add_sub_field is becoming quite a huge issue.

    DATABASE OUTPUT:

    Please see attached screenshot

    CODE:

    $data['rules'] = array(
    	array(
    		'referrer' => 1,
    		'rule' => 13,
    		'amount' => 100
    	),
    	array(
    		'referrer' => 2,
    		'rule' => 13,
    		'amount' => 10
    	)
    );
    
    update_post_meta( $post_id, 'referrers', count( $data['rules'] ) );
    
    $i = count( $data['rules'] );
    foreach ( $data['rules'] as $row ) {
    	update_sub_field( array( 'referrers', $i, 'contact_id' ), $row['referrer'], $post_id );
    	update_sub_field( array( 'referrers', $i, 'rule' ), $row['rule'], $post_id );
    	update_sub_field( array( 'referrers', $i, 'amount' ), $row['amount'], $post_id );
    	$i--;
    }

    I’ve attempted a separate solution, which works, but only partially. The issue primarily is that when you try and retrieve the repeater field get_field('referrers', $post_id) the output will be the count and not an array of the subfields. My attempted solution:

    CODE:

    $data['rules'] = array(
    	array(
    		'referrer' => 1,
    		'rule' => 13,
    		'amount' => 100
    	),
    	array(
    		'referrer' => 2,
    		'rule' => 13,
    		'amount' => 10
    	)
    );
    
    update_post_meta( $post_id, 'referrers', count( $data['rules'] ) );
    
    foreach ( $data['rules'] as $key => $row ) {
    	update_post_meta( $post_id, "referrers_{$key}_contact_id", $row['referrer'] );
    	update_post_meta( $post_id, "referrers_{$key}_rule", $row['rule'] );
    	update_post_meta( $post_id, "referrers_{$key}_amount", $row['amount'] );
    }

    Any help what-so-ever is highly appreciated. Thank you.

  • The problem with your workaround is that you’re not updating adding the needed field_key in addition to your value. For more information on field keys see this page http://www.advancedcustomfields.com/resources/update_field/

    http://www.advancedcustomfields.com/resources/update_sub_field/
    Your first example I think that you’re using update_sub_fields wrong, but I’m not sure. When I’m inserting new data into a repeater I generally go with your second example and insert the keys.

    Also, if you use update field on the repeater field itself and provide the correct array it will add the sub fields for you, for example:

    
    $repeater_value = array(
      // a nested array for each row
      array(
        // an element for each field
        'subfield_1' => 'value 1',
        'subfield_2' => 'value 2'
      ),
      // a nested array for each row
      array(
        // an element for each field
        'subfield_1' => 'value 1',
        'subfield_2' => 'value 2'
      ),
    )
    update_field('repeater_field', $repeater_value);
    
  • Hey John,

    Please ignore my rambling up the top. I didn’t realize you were meant to use Field keys, which wasn’t apparent as they were hidden away by default. I had figured this out shortly after posting the above.

    Thanks for the help either way! 🙂

  • Yes, field keys are needed if the field does not already exist.

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

The topic ‘Create New Repeater Subfield row through code’ is closed to new replies.