Home › Forums › Backend Issues (wp-admin) › Saving dynamically populated group sub fields
I have a custom post type “Client”, and a custom taxonomy “Region”. I would like to set a different “logo weight” value for each client’s region, and store it in a region_weights array.
I created a region_weights
ACF field of type “Group”, into which i want to dynamically include the sub fields (1 per region attached to the client).
define('REGION_WEIGHTS_FIELD_KEY', 'field_6668114add626');
add_filter('acf/prepare_field/key=' . REGION_WEIGHTS_FIELD_KEY, 'prepare_region_weights_field');
function prepare_region_weights_field($field)
{
global $post;
// Only apply this to the 'region_weights' field group
if ($field['_name'] !== 'region_weights' || $post->post_type !== 'client') {
return $field;
}
// Get the regions associated with this client
$regions = wp_get_post_terms($post->ID, 'region');
if (is_wp_error($regions) || empty($regions)) {
return false; // No regions, no fields to display
}
$field_value = get_post_meta($post->ID, REGION_WEIGHTS_FIELD_KEY, true);
// Generate a field for each region
foreach ($regions as $region) {
$field_name = 'field_region_weights_' . $region->term_id;
$default_value = isset($field_value[$field_name]) ? $field_value[$field_name] : 0;
$region_field = array(
'key' => $field_name,
'label' => $region->name . ' Logo Weight',
'name' => $field_name, // Use the correct field name
'type' => 'select',
'instructions' => 'How often should the ' . $region->name . ' logo appear?',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array(
0 => 'Logo should not appear',
1 => 'Logo should appear once in a while',
2 => 'Logo should appear pretty often',
3 => 'Logo should appear most often',
),
'default_value' => $default_value,
'value' => $default_value, // Use the correctly formatted meta key for the value
'_name' => 'region_weights',
'parent' => $field['key'],
'_valid' => 1,
);
$field['sub_fields'][] = $region_field;
}
// Replace the original field with the generated fields
return $field;
}
That part works. What does not work is that i cannot save the fields. The update_field function I attached to the ‘acf/save_post’ action hook returns false and I fail to see why. Can you help ?
add_action('save_post', 'save_client_regionWeights');
function save_client_regionWeights($post_id)
{
// Check if we're saving a 'client' post type
if (get_post_type($post_id) !== 'client') {
return;
}
// Check user permissions
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Check if it's an autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Check if ACF data is being saved
if (!isset($_POST['acf'])) {
return;
}
if (!isset($_POST['acf'][REGION_WEIGHTS_FIELD_KEY])) {
return;
}
// Get the regions associated with this client
$regions = wp_get_post_terms($post_id, 'region');
if (is_wp_error($regions) || empty($regions)) {
// No regions, nothing to save
return;
}
// Prepare an array to hold our region weights data
$region_weights_data = array();
// Loop through each region and collect the weight data from $_POST
foreach ($regions as $region) {
$field_name = 'field_region_weights_' . $region->term_id;
if (isset($_POST['acf'][REGION_WEIGHTS_FIELD_KEY][$field_name])) {
// Add this region's weight to our data array
$region_weights_data[$field_name] = $_POST['acf'][REGION_WEIGHTS_FIELD_KEY][$field_name];
} else {
// If no weight was set, default to 0
$region_weights_data[$field_name] = 0;
}
}
// Update the region weights data
update_field(REGION_WEIGHTS_FIELD_KEY, $region_weights_data, $post_id);
}
If I print the content of $_POST[‘acf’] I get sensible data:
Array
(
[field_646f1e398ee52] => Client Something
[field_63c684c9f2e59] => 95926
[field_63c93f5d38405] => 95921
[field_65cde112bc88c] => 2
[field_6668114add626] => Array
(
[field_region_weights_315] => 2
)
)
What am I doing wrong ?
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.