Support

Account

Home Forums General Issues Get specific sub group and count word occurrences in foreach loop

Helping

Get specific sub group and count word occurrences in foreach loop

  • I have an ACF field group with a key of group_65e26ad7b9a44. We’ll call this the Main group. This specific Main group has 10 fields within it, all of the type Group. We’ll call this the Sub groups. Each Sub group has a checkbox field with in called ‘status’. The checkbox is configured with the following choices: ‘Open’ and ‘Snow’. The hierarchy looks like this:

    Main Group (group_65e26ad7b9a44)
    Group field1
    Checkbox field (name=status)
    Choices: open, Snow
    Group field2
    Checkbox field (name=status)
    Choices: open, Snow
    Group field3
    Checkbox field (name=status)
    Choices: open, Snow
    etc. etc.

    I’m trying to write a PHP function that will loop through the Main group, then get the field values of the checkbox field for each Sub group. IF the word ‘Day’ is found in the value then increment a counter +1.

    Here is the code I have so far:


    add_shortcode('count_runs', 'count_open_run_occurrences');

    function count_open_run_occurrences() {
    $field_group_key = 'group_65e26ad7b9a44';
    $fields = acf_get_fields($field_group_key);
    $open_count = 0;
    $total_fields = count($fields);
    foreach ($fields as $field) {
    $field_value = get_field($field['key']);
    if (is_array($field_value)) {
    foreach ($field_value as $checkbox_value) {
    if (stripos($checkbox_value, 'Open') !== false) {
    $open_count++;
    break;
    }
    }
    }
    }
    return "Lifts {$open_count}/{$total_fields} open";
    }

    The above code works ONLY IF the checkbox field is NOT within a Sub group. It works if the checkbox field is a top level field within the Main group. I’ve been trying to adapt it to work within a Sub Group. The $total_fields part works fine however.

    I’ve tried the following and dozens of other variants:


    add_shortcode('count_runs', 'count_open_day_shortcode');

    function count_open_day_shortcode() {
    $group_key = 'group_65e26ad7b9a44';
    $open_day_count = 0;
    $primary_group = acf_get_fields($group_key);

    if ($primary_group) {
    foreach ($primary_group as $sub_group) {
    $status_values = $sub_group['status'];
    if (is_array($status_values) && in_array('Open Day', $status_values)) {
    $open_day_count++;
    }
    }
    }
    return "Total occurrences of 'Open Day': $open_day_count";
    }

    No matter what I do, I get a counter value of 0 when the checkbox field is within a sub group.

    What are the fields for and the purpose of this function?

    This function is used on a site that is for a ski resort. The function will loop through all ski runs (group fields) and look at the sub Status field which is a checkbox field and look for the choice of ‘Open’ selected. If ‘Open’ is found then $open_count++;

  • I’ve also tried using the have_rows method as seen below, no luck.

    
    function show_status() {
        $field_group_key = 'group_65e26ad7b9a44';
        $fields = acf_get_fields($field_group_key);
        $open_count = 0;
        $total_fields = count($fields);
    
        foreach ($fields as $field) {
    		if (have_rows($field)) {
    			while (have_rows($field) ) {
    				the_row(); 
    				$status = get_sub_field('status');		      
    				foreach ($status as $checkbox_value) {
    					if (stripos($checkbox_value, 'Open') !== false) {
    						$open_count++;
    						break;
                    	                 }
    				}
                           }
                 }
        }
        return "Runs {$open_count}/{$total_fields} open";
    }
    // Register the shortcode
    add_shortcode('show_the_status', 'show_status');
    

    Nothing I have tried will seem to grab onto the sub field status and parse the checkbox array and up the counter.

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

You must be logged in to reply to this topic.