Support

Account

Home Forums ACF PRO if (have_rows()) not working Reply To: if (have_rows()) not working

  • Hi guys

    Thanks for the bug report. I’ve found and fixed the issue. You can expect an update shortly, but for now, please find and replace the have_rows() function within the api-template.php file:

    
    /*
    *  have_rows
    *
    *  This function will instantiate a global variable containing the rows of a repeater or flexible content field,
    *  afterwhich, it will determin if another row exists to loop through
    *
    *  @type	function
    *  @date	2/09/13
    *  @since	4.3.0
    *
    *  @param	$field_name (string) the field name
    *  @param	$post_id (mixed) the post_id of which the value is saved against
    *  @return	(boolean)
    */
    
    function have_rows( $selector, $post_id = false ) {
    	
    	// vars
    	$row = array();
    	$new_parent_loop = false;
    	$new_child_loop = false;
    	$sub_field = false;
    	$sub_exists = false;
    	
    	
    	// reference
    	$_post_id = $post_id;
    	
    	
    	// filter post_id
    	$post_id = acf_get_valid_post_id( $post_id );
    	
    	
    	// empty?
    	if( empty($GLOBALS['acf_field']) ) {
    		
    		// reset
    		reset_rows( true );
    		
    		
    		// create a new loop
    		$new_parent_loop = true;
    	
    	} else {
    		
    		// vars
    		$row = end( $GLOBALS['acf_field'] );
    		$prev = prev( $GLOBALS['acf_field'] );
    		$change = false;
    		
    		
    		// detect change
    		if( $post_id != $row['post_id'] ) {
    			
    			$change = 'post_id';
    				
    		} elseif( $selector != $row['selector'] ) {
    			
    			$change = 'selector';
    				
    		}
    		
    		
    		// attempt to find sub field
    		if( $change ) {
    			
    			$sub_field = acf_get_sub_field($selector, $row['field']);
    			
    			if( $sub_field ) {
    				
    				$sub_exists = isset($row['value'][ $row['i'] ][ $sub_field['key'] ]);
    				
    			}
    			
    		}
    		
    		
    		// If post_id has changed, this is most likely an archive loop
    		if( $change == 'post_id' ) {
    			
    			if( $prev && $prev['post_id'] == $post_id ) {
    				
    				// case: Change in $post_id was due to a nested loop ending
    				// action: move up one level through the loops
    				reset_rows();
    			
    			} elseif( empty($_post_id) && $sub_exists ) {
    				
    				// case: Change in $post_id was due to this being a nested loop and not specifying the $post_id
    				// action: move down one level into a new loop
    				$new_child_loop = true;
    			
    			} else {
    				
    				// case: Chang in $post_id is the most obvious, used in an WP_Query loop with multiple $post objects
    				// action: leave this current loop alone and create a new parent loop
    				$new_parent_loop = true;
    				
    			}
    			
    		} elseif( $change == 'selector' ) {
    			
    			if( $prev && $prev['selector'] == $selector && $prev['post_id'] == $post_id ) {
    				
    				// case: Change in $field_name was due to a nested loop ending
    				// action: move up one level through the loops
    				reset_rows();
    				
    			} elseif( $sub_exists ) {
    				
    				// case: Change in $field_name was due to this being a nested loop
    				// action: move down one level into a new loop
    				$new_child_loop = true;
    				
    			} else {
    				
    				// case: Chang in $field_name is the most obvious, this is a new loop for a different field within the $post
    				// action: leave this current loop alone and create a new parent loop
    				$new_parent_loop = true;
    				
    			}
    			
    		}
    		
    	}
    	
    	
    	if( $new_parent_loop ) {
    		
    		// vars
    		$field = get_field_object( $selector, $post_id, false );
    		$value = acf_extract_var( $field, 'value' );
    		
    		
    		// add row
    		$GLOBALS['acf_field'][] = array(
    			'selector'	=> $selector,
    			'value'		=> $value,
    			'field'		=> $field,
    			'i'			=> -1,
    			'post_id'	=> $post_id,
    		);
    		
    	} elseif( $new_child_loop ) {
    		
    		// vars
    		$value = $row['value'][ $row['i'] ][ $sub_field['key'] ];
    		
    		$GLOBALS['acf_field'][] = array(
    			'selector'	=> $selector,
    			'value'		=> $value,
    			'field'		=> $sub_field,
    			'i'			=> -1,
    			'post_id'	=> $post_id,
    		);
    		
    	}	
    	
    	
    	// update vars
    	$row = end( $GLOBALS['acf_field'] );
    	
    	
    	
    	// return true if next row exists
    	if( is_array($row['value']) && array_key_exists($row['i']+1, $row['value']) ) {
    		
    		return true;
    		
    	}
    	
    	
    	// no next row!
    	reset_rows();
    	
    	
    	// return
    	return false;
      
    }
    

    Please let me know how this fix goes