Support

Account

Home Forums ACF PRO Counting Repeater rows sharing same subfield value Reply To: Counting Repeater rows sharing same subfield value

  • The repeater field stores the values as a serialized array in the database so I don’t think it will be easy to get an exact count outside of the repeater loop. If you need discreet access to this data I would use the acf/update_value and acf/delete_value filters to maintain the counts in different meta fields.

    
    $field_key = 'your field key'; // or use name, type, etc and the proper filters
    
    // Run on field value update
    add_filter( 'acf/update_value/key={$field_key}', function($values, $post_id, $field){
    	if ( $field['type'] == 'repeater' ){
    		// you might need to delete "old" value sums in the db that aren't on the current values
    		$sums = array();
    		foreach( $values as $value ){
    			if ( !isset( $sums[$value] ) ){
    				$sums[$value] = 1;
    			} else {
    				$sums[$value]++;
    			}
    		}
    
    		foreach( $sums as $value => $sum ){
    			// save the sum for each value in a different field
    		}
    	}
    }, 20, 3 );
    
    // Run when a value is deleted
    add_filter( 'acf/delete_value/key={$field_key}', function($value, $post_id, $field){
    	// probably do the exact same thing as when a value is updated... recalculate the sum of each value
    }, 20, 3 );