Support

Account

Home Forums Add-ons Repeater Field Repeater only outputting the last row

Solved

Repeater only outputting the last row

  • Im following example 2 from these instructions to dynamically populate a select field.

    I have a repeater setup which contains a text field. The content of this field should populate the select field. But only the last row is being output.

    Here is the code I’ve used in my functions file.

    function acf_load_opposition_player_choices( $field ) {
    
    	// reset choices
    	$field['choices'] = array();
    
    	// if has rows
    	if ( have_rows( 'opposition_squad' ) ) {
    
    		// while has rows
    		while ( have_rows( 'opposition_squad' ) ) {
    
    			// instantiate row
    			the_row();
    
    			// vars
    			$value = get_sub_field( 'opposition_player' );
    
    			echo '<pre>';
    			print_r( $value );
    			echo '</pre>';
    
    			// append to choices
    			$field['choices'][ $value ] = $value;
    
    		}
    	}
    
    	// return the field
    	return $field;
    }
    
    add_filter( 'acf/load_field/name=opposition_team_event', 'acf_load_opposition_player_choices' );

    To check content is coming from the repeater i did this

    $rows = get_field( 'opposition_squad' );

    and printed the results…

    Array
    (
        [0] => Array
            (
                [opposition_player] => Stefan Ratchford
            )
    
        [1] => Array
            (
                [opposition_player] => Matthew Russell
            )
    
        [2] => Array
            (
                [opposition_player] => Rhys Evans
            )
    
        [3] => Array
            (
                [opposition_player] => Toby King
            )
    
        [4] => Array
            (
                [opposition_player] => Jack Johnson
            )
    
        [5] => Array
            (
                [opposition_player] => Kevin Brown
            )
    
        [6] => Array
            (
                [opposition_player] => Kurt Gidley
            )
    
        [7] => Array
            (
                [opposition_player] => Chris Hill
            )
    
        [8] => Array
            (
                [opposition_player] => Daryl Clark
            )
    
        [9] => Array
            (
                [opposition_player] => Mike Cooper
            )
    
        [10] => Array
            (
                [opposition_player] => Ben Westwood
            )
    
        [11] => Array
            (
                [opposition_player] => Jack Hughes
            )
    
        [12] => Array
            (
                [opposition_player] => Joe Westerman
            )
    
        [13] => Array
            (
                [opposition_player] => Ashton Sims
            )
    
        [14] => Array
            (
                [opposition_player] => Brad Dwyer
            )
    
        [15] => Array
            (
                [opposition_player] => Dominic Crosby
            )
    
        [16] => Array
            (
                [opposition_player] => Harvey Livett
            )
    
        [17] => Array
            (
                [opposition_player] => Test
            )
    
        [18] => Array
            (
                [opposition_player] => Testing
            )
    
    )

    Any help appreciated

  • What field is opposition_team_event. Is the a repeater, a sub field of a repeater or a top level field?

  • That is a top level field associated with the comment field. The field is a select

  • Is the repeater field you’re getting the choices from a field on the same post as the field you want to populate?

  • Yes they are both associated with a custom post type post.

    I have the same setup working with another repeater and select. But this repeater contains a post object. This is reflected with the slight difference in the below code. The count in this setup just ignores the last two rows. If that is removed all rows are returned.

    function acf_load_player_choices( $field ) {
    
    	// reset choices
    	$field['choices'] = array();
    
    	// if has rows
    	if ( have_rows( 'saints_squad' ) ) {
    		$i = 1;
    		// while has rows
    		while ( have_rows( 'saints_squad' ) ) {
    
    			// instantiate row
    			the_row();
    
    			// vars
    			$value = get_sub_field( 'player' );
    			$post = $value;
    			setup_postdata( $post );
    
    			if ( $i < 18 ) {
    
    				// append to choices
    				$field['choices'][ $post->post_title ] = $post->post_title;
    			}
    
    			wp_reset_postdata();
    
    			$i++;
    		}
    	}
    
    	// return the field
    	return $field;
    }
    
    add_filter( 'acf/load_field/name=saints_team_event', 'acf_load_player_choices' );

    But this setup works.

    Is there something I’m missing in the above that would cause problems with a second one?

  • I honestly don’t see anything “wrong” with your code in the first example. But I have had problems in the paste in load field filters when getting values from the same post that is being loaded. I just think that there are cases where calling ACF functions causes ACF to interfere with itself.

    As a work-a-round you can try using get_post_meta to get the value, but I’m not sure that will work either.

    
    // we need the post ID for get_post_meta
    $post_id = $_GET['post']; // should have post id
    $count = intval(get_post_meta($post_id, 'opposition_squad', true)); // holds $ rows in repeater
    for (i=0; i<$count; i++) {
      // foreach row of the repeater
      $value = get_post_meta($post_id, 'opposition_squad_.'$i.'_opposition_player', true);
      $choices[$value] = $value;
    }
    
  • John, thank you for taking the time to help, it is really appreciated.
    I was getting errors with your code, but it prompted me to take another look at something i had been trying using get_field() instead.

    This is what i have now, which is working

    function acf_load_opposition_player_choices( $field ) {
    
    	// reset choices
    	$field['choices'] = array();
    
    	$rows = get_field( 'opposition_squad' );
    
    	//echo '<pre>';
    	//print_r( $rows );
    	//echo '</pre>';
    
    	// if has rows
    	if ( $rows ) {
    		$i = 1;
    		foreach ( $rows as $row ) {
    
    			//echo $row['opposition_player'];
    			$value = $row['opposition_player'];
    			//echo $value;
    			if ( $i < 18 ) {
    				$field['choices'][ $value ] = $value;
    			}
    			$i++;
    		}
    	}
    	//echo '<pre>';
    	//print_r( $field );
    	//echo '</pre>';
    
    	// return the field
    	return $field;
    
    }
    
    add_filter( 'acf/load_field/name=opposition_team_event', 'acf_load_opposition_player_choices' );

    I don’t really understand why this would work over the documented method, but its working 😉

    Thanks again

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

The topic ‘Repeater only outputting the last row’ is closed to new replies.