Support

Account

Home Forums General Issues Updating a repeater field

Solved

Updating a repeater field

  • Can anyone help please – this is driving me potty!
    I have read lots of examples which take an array and update a repeater field and I’m sure my code follows their advice but… nothing updates. Here’s my code

    `$imageurls = array_column( $vehicle->{ ‘Images’}, ‘ImageURI’);

    foreach( $imageurls as $url ) {
    $images[] = array(
    array(‘field_5e70d211200ab’ => $url),
    );
    }`

    and a var dump of $imageurls reveals this

    array(5) { [0]=> array(1) { ["field_5e70d223200ab"]=> string(166) "https://example.com" } [1]=> array(1) { ["field_5e70d223200ab"]=> string(166) "https://example.com" } [2]=> array(1) { ["field_5e70d223200ab"]=> string(166) "https://example.com" } [3]=> array(1) { ["field_5e70d223200ab"]=> string(166) "https://example.com" } [4]=> array(1) { ["field_5e70d223200ab"]=> string(166) "https://example.com" } }

    so I would have expected the repeater field to hold the list of 5 URLs but theres nothing in it.
    The repeater field has field key field_5e70d211200aa and the sub field (url) has key field_5e70d223200ab if that helps

    Thanks in advance

  • What does your update_field() call look like?

  • Oh sorry John, thought I had included that – here it is

    update_field( ‘images’, $images, $post_id );

    I’ve also tried it with the field key for the repeater field

    Bit of an update – it now creates 5 blank entries in the repeater!

    Cheers

  • use the field key

    
    update_field( 'field_5e70d211200aa', $images, $post_id );
    
  • Thanks John

    I tried that but still nothing – here’s my output screen

  • You said that the sub field is a url field, double check this.

    Also double check that the urls are valid.

    Also double check the field key of the sub field.

    Your rows are being added, just not the sub fields so any of the above being wrong could cause this.

  • Thanks for that John – I gave up and went home just after you sent it so didn’t reply 🙂
    I know its something silly I’m doing but this works and appends a single item

    	$imageurls = array_column( $vehicle->{ 'Images'}, 'ImageURI');
    
                foreach( $imageurls as $url ) {
                    $images= array(
    		        array('field_5e70d223200ab' => $url)
    		);
    	}
    			

    Its just giving 5 blank entries when $images is set to be an array ie $images[] = …

  • Woah! Cracked it thanks for your help John – turns out because the images are in an array I dont need a nested array in update_field – so this works!

     foreach( $imageurls as $url ) {
                    $carpic[]=
    				    array('field_5e70d223200ab' => $url);
    			}
    
    			update_field( 'field_5e70d211200aa', $carpic, $post_id); 

    Knew it would be my fault 🙂

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

The topic ‘Updating a repeater field’ is closed to new replies.