0

I have set up an ACF Clone Field called 'NJ' in Field Group 'NJ', which clones all of the 70+ ACF fields in Field Group 'Global'. These field groups appear on the same custom post type.

I would like for these cloned fields to have a default/fallback value set, which is equal to the value in the original field. So that if the cloned field is left blank, it will default to the value in the original field. Which will in turn, remove the need for lots of conditional statements in my template code.

Example:

  • 'Global' Field Group > Number Field: 'Total Games' > Value: '100'

  • 'NJ' Field Group > Number Field (created by Clone Field): 'Total Games' > (if no value is input, use value from Global Field Group on the same post)

I found a couple of answers on here that copy the values when post is saved, but it's not quite what I'm looking for. It may not be possible I know but any advice would be appreciated.

Thanks in advance!

1
  • Add this php to your post echo '<pre>'; print_r(get_fields()); echo '</pre>'; exit; and add the returned array to your question.
    – joshmoto
    Jun 21, 2020 at 21:55

1 Answer 1

1

Based on your pastebin array, this code below should loop through all your values in NJ, check if any of the values are empty, and if values have subfields or if value type is a post object.

For example on NJ [welcome_bonus], is an array with sub fields. This code will loop through each subfield in [NJ][welcome_bonus], check if field is blank, if it is blank, it will then get the original field value and set it as that.

You don't need Global 2 group, unless you are using this for something else.

See comments in code...

// 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);

    }

}
11
  • Thank you for this answer, simply incredible. Running the field dump gave me this: Array ( // fields in original 'Global' field group [nj] => Array ( // cloned fields in 'NJ' field group ) ) Would it be possible/advisable to adapt your code so that it works without the [global] selector? Alternatively, I can make another Field Group containing a clone called 'Global'. When I tested, this returned the array you specified. If needed I can use that and simply hide the original. Which approach do you advise?
    – Alex
    Jun 22, 2020 at 9:28
  • Ah so all your global fields are not in a group? They are just normal fields, not sub fields? So NJ group is an amalgamation of you fields in you post?
    – joshmoto
    Jun 22, 2020 at 12:20
  • NJ group is created using the ACF Clone Field. Clone Field automatically turns itself into the fields you've asked it to clone, and (apparently) places them into a group (in this case, NJ). The original field group 'Global' isn't a clone so doesn't have this grouping. What I was suggesting is I could create a new 'Global' field group which is Cloned from 'Global', and use that as the new 'Global', which (I think) would make your solution work. But before I did that, wanted to check if that's indeed the best/only way.
    – Alex
    Jun 22, 2020 at 12:50
  • Hmmm. Can you actually post the array output formatted in a fiddle or something for an example post? I've updated the question but not sure thats guna work for you now.
    – joshmoto
    Jun 22, 2020 at 12:54
  • Pasted the output here, and commented where each group begins - pastebin.com/qaZBh10B. I tested both of your approaches by adding them via the Code Snippets plugin (I'm using Oxygen Builder which disables functions.php), then adding data to [global] and [global_2] and saving to see if the data populated the empty NJ fields. Unfortunately, no result yet. Thanks for your ongoing help, and sorry for the slow reply.
    – Alex
    Jun 22, 2020 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.