Support

Account

Home Forums Add-ons Repeater Field Removing a value from Repeater's sub field's array programatically

Helping

Removing a value from Repeater's sub field's array programatically

  • Hello,

    I have a Repeater-type field named referral_code having a Text-type field named referral_value and the field group is attached to Options page.

    I have managed to add a new field in WooCommerce user registration form to and validate against the values for the above Text field.

    Now I am trying to remove one of the values if it has been used during WooCommerce registration.

    In other words, need a way to programatically remove a value from Repeater’s sub field’s array.

    If anyone can share sample code (even if it is showing how to remove one hard-coded text value) for this I would appreciate it.

    Something along the lines of this non-working code:

    add_filter( 'acf/load_value/name=referral_value', 'my_acf_load_method', 10, 3 );
    /**
     * Remove a value from Repeater's sub field's array.
     *
     * @param mixed $value The field value.
     * @param int|string $post_id The post ID where the value is saved.
     * @param array $field The field array containing all settings.
     * @return $value Modified value.
     */
    function my_acf_load_method( $value, $post_id, $field ) {
    	unset( $value['acX54s'] );
    	return $value;
    }
  • This looks more like it:

    // check if the repeater field has rows of data.
    if ( have_rows( 'referral_code', 'option' ) ) {
    	// loop through the rows of data.
    	while ( have_rows( 'referral_code', 'option' ) ) : the_row();
    		if ( 'acX54s' === get_sub_field( 'referral_value' ) ) {
    			delete_sub_row( 'referral_value', get_row_index() );
    		}
    	endwhile;
    } else {
    	// no rows found.
    }
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Removing a value from Repeater's sub field's array programatically’ is closed to new replies.