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

    • panda11

    • November 22, 2019 at 5:44 pm

    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?

    • panda11

    • November 23, 2019 at 5:39 am

    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;
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.