Support

Account

Home Forums Add-ons Repeater Field Create New Repeater Row from Front End Form Reply To: Create New Repeater Row from Front End Form

  • I believe you’re right, because I’m using the below code and not having to do any weird math like that.

    //Add a row with the user's info as an admin to the Staff repeater field
    			//There are no rows yet, so we update the value of the repeater field to 1 to make a new row
    			//The value of the repeater field is simply a number representing the number of existing row
    			//If ACF ever changes the value of the repeater field in the database, this will need adjusting
    			if (get_post_meta($post_id, 'choose_tps_members')) {
    				$existingRows = get_post_meta($post_id, 'choose_tps_members', true);
    				if (have_rows('choose_tps_members', $post_id)) {
    						$selectedMembers = array();
    					while(have_rows('choose_tps_members', $post_id)) {
    						the_row();
    						$selectedMembers[] = get_sub_field('choose_member'); //user array
    					}
    				}
    				
    			}
    			//Add a row to the repeater
    			update_post_meta($post_id, 'choose_tps_members', $existingRows+1);
    			
    			//Update repeater row we just created with the Creator's info
    			$setUser = get_user_by('id',$creator);
    			$row = array(
    				'choose_member'	=> $setUser, //Creator
    				'make_admin'	=> array('Admin'), //Check the "make admin" box
    			);
    			//Update the repeater row
    			update_row( 'choose_tps_members', $existingRows+1, $row, $post_id );
    			
    			//Once all fields validate, we can finally publish it
    			wp_update_post(array('ID'=>$post_id, 'post_status'=>'publish'));