Hello!
Tell me how to count the number of ACF Repeater fields with a specific value?
I tried to do:
$total = 0;
$field_name = get_field('field_name');
if ($field_name) {
foreach ($field_name as $row) {
if ($row['sub_field_name'] == 'My value') {
$total++;
}
}
}
echo $total;
But I get 0. Although, if instead of “My value” is set aside, then it displays the number of fields that are not filled.
What type of field is the field you’re trying to check?
John Huebner, thanks for feedback, I resolve this problem.
$total = 0;
$repeater = get_field('repeater');
if ($repeater) {
foreach ($repeater as $row) {
if (is_array($row['sub_field_name']) && $row['sub_field_name']['value'] == 'Value of field') {
$total++;
}
}
}
echo $total;
For future reference, this type of functionality is included in the plugin “Calculated fields for ACF”. Available for free on the WordPress plugin repo.