Support

Account

Home Forums General Issues Total Number of Checkboxes Checked Reply To: Total Number of Checkboxes Checked

  • Went a different route.

    // Hook to save_post action
    add_action('save_post', 'update_number_field');
    
    function update_number_field($post_id) {
        // Check if it's an autosave
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return;
        }
    
        // Check if the user has permission to edit the post
        if (!current_user_can('edit_post', $post_id)) {
            return;
        }
    
        // Check if the post type is the one you want to target (e.g., 'post' or 'page')
        $post_type = get_post_type($post_id);
        if ($post_type != 'team') {
            return;
        }
    
        // Get the values of the checkbox field
        $checkbox_values = get_post_meta($post_id, 'state_title_years_won', true);
    
        // Calculate the number of checked checkboxes
        $number_field_value = is_array($checkbox_values) ? count($checkbox_values) : 0;
    
        // Update the number field
        update_post_meta($post_id, 'total_state_championships_won', $number_field_value);
    }