基于您的pastebin array, 下面的代码应该循环遍历NJ中的所有值,检查是否有任何值为空,值是否有子字段,或者值类型是否为post对象。
例如在NJ上[welcome_bonus]
, 是具有子字段的数组。此代码将循环通过中的每个子字段[NJ][welcome_bonus]
, 检查字段是否为空,如果为空,则会获取原始字段值并将其设置为空。
您不需要Global 2 group,除非您将其用于其他用途。
查看代码中的注释。。。
// acf action that runs when you save post
add_action( \'acf/save_post\', \'nj_empty_clone_value_check\', 20 );
/**
* @param $post_id int
* @return void
*/
function nj_empty_clone_value_check($post_id) {
// global post
global $post;
// check we are on the correct post type (casino_review)
if($post->post_type <> \'casino_review\') return;
// get all the fields values from our current post
$fields = get_fields($post_id);
// changes check
$changes = false;
// for each of the cloned fields in NJ group
foreach ($fields[\'nj\'] as $key => $value) {
// if current NJ cloned field is a post object
if(is_object($value)) {
// temp cast
$temp = (array)$value;
// check if post object is not empty (blank)
if(!empty($temp)) {
// update the NJ cloned field value with the original field value
$fields[\'nj\'][$key] = $fields[$key];
// update changes check to true
$changes = true;
}
}
// else if current NJ cloned field is an array (has subfields)
else if(is_array($value)) {
// for each of the cloned array subfields in NJ group
foreach ($value as $sub_key => $sub_value) {
// if current NJ cloned subfield value is not blank
if (!$sub_value) {
// check the original subfield value is not blank
if (!empty($fields[$key][$sub_key])) {
// update the NJ group subfield value with the original field value
$fields[\'nj\'][$key][$sub_key] = $fields[$key][$sub_key];
// update changes check to true
$changes = true;
}
}
}
}
// if current NJ cloned field value is not blank
else if(!$value) {
// check the original field value is not blank
if(!empty($fields[$key])) {
// update the NJ field value with the original field value
$fields[\'nj\'][$key] = $fields[$key];
// update changes check to true
$changes = true;
}
}
}
// if any changes have been made to NJ group
if($changes) {
// update NJ group field with updated NJ array data
update_field(\'nj\', $fields[\'nj\'], $post_id);
}
}