Support

Account

Home Forums Add-ons Repeater Field Getting a count of repeater sub fields with specific value

Solved

Getting a count of repeater sub fields with specific value

  • 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.

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

You must be logged in to reply to this topic.