Support

Account

Home Forums General Issues Custom acf/load_value function issues

Unread

Custom acf/load_value function issues

  • I’m trying to write a custom version of the backend repeater sorting code here: https://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/

    I’m grabbing the repeater name and sorting field key dynamically.

    function my_acf_load_value($value, $post_id, $field, $subfield_key) {
    	return function () use ($value, $post_id, $field, $subfield_key) {
            	// vars
    		$order = array();
    
            	// bail early if no value
    		if( empty($value) ) {
    
    			return $value;
    
    		}
    
            	// populate order
    		foreach( $value as $i => $row ) {
    			$order[ $i ] = $row[$subfield_key];
    		}
    
            	// multisort
    		array_multisort( $order, SORT_DESC, $value );
    
            	// return   
    		return $value;
    	};
    }
    
    function acu_standing_order() {
    	
    	$standings_args = array(
    		'posts_per_page' => -1,
    		'post_type' => 'standings'
    	);
    
    	$standings = new WP_Query( $standings_args );
    
    	if($standings->have_posts()){
    		while($standings->have_posts()) {
    
    			$standings->the_post();
    
    			$post_id = get_the_ID();
    			
    			$fields = get_fields($post_id);
    			
    			if ($fields) {
    				foreach ($fields as $field_name => $value) {
    					// Get field object meta so we can check type
    					$field_object = get_field_object($field_name, $post_id);
    					
    					// Find repeater fields
    					if ($field_object && $field_object['type'] === 'repeater') {
    						$field_name = $field_object['name'];
    
    						if (have_rows($field_name, $post_id)) {
    							while (have_rows($field_name, $post_id)) { the_row();
    
    								// $subfield_object = get_sub_field_object('points');
    								// $subfield_key = $subfield_object['key'];
    
    								if(have_rows('points')){
    
    									// Iterate points fields
    									while(have_rows('points')) : the_row();
    										$subfield_object = get_sub_field_object('total');
    										$subfield_key = $subfield_object['key'];
    									endwhile; // points group
    
    								}
    
    							}
    						}
    
    						add_filter('acf/load_value/name=' . $field_name, my_acf_load_value($value, $post_id, $field_name, $subfield_key), 10, 3);
    					}
    				}
    			}
    		}
    	}
    }

    Currently getting the error…

    Warning: Undefined array key “field_68221235bbc48” in Path/functions.php on line 419

    So it looks like I’m getting the right key but something else is wrong.

    Help appreciated.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.