Support

Account

Home Forums Add-ons Repeater Field Only last repeater entry is accessible in my loop?

Solved

Only last repeater entry is accessible in my loop?

  • Hi there-

    I have set up a Repeater field on an Options Page. The repeater allows the user to select some attributes and apply them to a particular post or group of posts through a Post Object field.

    Then, when looping through posts on a category page, I check each post against an array of Post IDs from the Post Object subfield, applying the selected values if the current post ID is present in the selected posts ID array.

    The problem I’m encountering now is that while checking the post IDs against the posts selected in the Repeater, it’s only checking against the posts selected in the last Repeater entry created.

    I need to be able to add Repeater entries that have different values for different post groups – am I doing something wrong here?

    I have a sinking feeling… It seemed simple – I guess I assumed that my loop would check all of the repeater rows and apply the values from the other subfields in that row to the post in question if it matched the ID array. However thinking about it, I guess I haven’t provided for there being multiple rows/arrays… Argh!!

    Can someone point me in the right direction? Any help would be very much appreciated. Below is my current code which currently works as far as the last-entered Repeater field.

    if(get_field('sales', 'option')):
     
    	while(has_sub_field('sales', 'option')):
     
    		$saletype = get_sub_field('sale_type');
    		$discount = get_sub_field('discount_amount');
    		$saleitems = get_sub_field('devices_on_sale');
    		$idlist = array();
    
    		if( $saleitems ):
    		    foreach( $saleitems as $post):
    		        setup_postdata($post);
    				$thisid = $post->ID;
    				$idlist[] = $thisid;
    		    endforeach;
    		    wp_reset_postdata();
    		endif;
    					 
    	endwhile;
     
    endif;
    
    $currentlistitempostid = $single->ID;
    
    if (in_array($currentlistitempostid, $idlist)) {
    	// IS PRESENT IN ARRAY - THIS ONLY CHECKS AGAINST THE MOST RECENT REPEATER ENTRY?
    }else{
    	// IS NOT PRESENT IN ARRAY
    }
  • Hi @cosmic
    It is most likely the setup_postdata function which is destroying the repeater field loop.

    Can you please remove this along with the wp_reset_postdata function and try again.

    Can you please debug your code? Simply print out the $idlist array to see if your id’s are being added.

  • Hi @cosmic,

    Your problem is that you’re declaring the $idlist array inside the while loop!

    This resets the array, and therefore only your last entry into it is recorded.

    Let me know if this helps!

  • Hi guys,

    Thanks for your help firstly! I’m really impressed with the quality of this plugin, I know I’m trying to do something tricky here with only a working knowledge of PHP. Everything else I’ve used ACF for (lots of applications!) works like a charm straight out of the box.

    Now.

    I tried removing the postdata functions, but that doesn’t seem to have changed anything.

    Taking the $idlist declaration out of the loop does seem to have helped- I set up two test repeater rows and posts chosen in both are being affected.

    However – only the values from the last repeater row are being applied. I.e. I have two sales, one takes $50 off a group of posts, the second takes $150. Now all the posts selected are showing $150 reduction.

    This is the current code I’ve put in a test page template to get the print:

    if(get_field('sales', 'option')):
     
    	while(has_sub_field('sales', 'option')):
     
    		$saletype = get_sub_field('sale_type');
    		$discount = get_sub_field('discount_amount');
    		$saleitems = get_sub_field('devices_on_sale');
    
    		if( $saleitems ):
    		    foreach( $saleitems as $post):
    				$thisid = $post->ID;
    				$idlist[] = $thisid;
    		    endforeach;
    		endif;
    		
    		echo($discount);
    		print_r($idlist);
     
    	endwhile;
     
    endif;

    Which yields:
    100Array ( [0] => 3391 [1] => 3410 [2] => 3412 ) 150Array ( [0] => 3391 [1] => 3410 [2] => 3412 [3] => 1292 [4] => 751 )

    So by my understanding, this is adding both groups of selected posts to the same array.

    Perhaps during the foreach loop, the $post->$ID can be stored differently… I have a vague idea – what I need is not a single named array, but some kind of anonymous array that can be referenced differently somehow, when I need it?

    Alternatively, could I create a “name” text subfield for each repeater row to provide each row with a unique identifier, then reference the related array from that somehow?

    Argh! Any further help much, much appreciated! Cheers for having such great support!

  • Thanks again for the help – I think I’ve solved the problem! It was all about handling the loops properly.

    Keep up the good work!

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

The topic ‘Only last repeater entry is accessible in my loop?’ is closed to new replies.