0

So I have a really complex ACF repeater / flexible content field (essentially a pagebuilder). What I want to do is get a specific sub-sub-subfield value from one post ($source_post) and insert it in the corresponding place in another post ($target_post) programmatically. I have a map that points to the subfield, in array and string form, it looks like this:

$fieldmap_array = array( 
    [2] => 0 
    [3] => columns 
    [4] => 1 
    [5] => layout_content 
    [6] => 0 
    [7] => image 
);
$fieldmap_string = [0][columns][1][layout_content][0][image];

(It is parsed from a string that I acquire dynamically via an ajax call).

Now I can get the value of that subfield from the original post like this:

$source_repeater_field_content = get_field('repeater_field', $source_post_id);
foreach ($fieldmap_array as $key) : 
    $source_repeater_field_content = $repeater_field_content[$key];
endforeach;

At the end of this loop $source_repeater_field_content will contain the value I am looking for.

What I want to do now is to insert that value in the corresponding place in the target post, so basically do this:

$target_repeater_field_content = get_field('repeater_field', $target_post_id);
$target_repeater_field_content[0]['columns'][1]['layout_content'][0]['image'] = $source_repeater_field_content;
update_field('repeater_field', $target_repeater_field_content, $target_post_id);

However I can't just type in a string that selects this correct subfield, I need to be able to dynamically point to the correct key in the target field array via a foreach loop or by some other means and I don't know how to do that.

I don't even know how to ask this question correctly :( I assume I can do it with array pointers somehow? Help?

(If you are curious about application, I want to add a button next to all image fields in this pagebuilder that automatically synchronizes the image between translations of a page (wpml). My client has a site in 5 languages and they complain that it is a lot of work to switch images for each language separately. Since this would be done AFTER the page itself is translated, it does not work to just sync the entire post content).

  • downvoted as this kind of questions make people believe that ACF is on-topic which is not – Mark Kaplun May 18 '16 at 19:17
  • Sorry, I had no idea. Can you suggest a more suitable place for this type of question? – Maija Vilkina May 18 '16 at 19:33
  • Probably the ACF support forums. It is probably ok on stackoverflow although I don't know how many people that can answer this kind of question hang out there – Mark Kaplun May 18 '16 at 20:06
  • Will keep in mind for my future questions. You are right, Stackexchange would have been more appropriate, it is essentially a php question anyway. – Maija Vilkina May 18 '16 at 20:11
0

Answering my own question again. :P I finally found this Gist that lets me get and set array variables via dot notation: https://gist.github.com/elfet/4713488

So I included the DotNotation class and now my code looks like this (skipping all sorts of error checking here) :

$fieldmap_string = 'key1.key2.key3.etc';
$current_page_content = get_field('page_content', $source_post_id);
$parsed_current_content = new DotNotation($current_page_content);
$image_field_value = $parsed_current_content->get($fieldmap_string);

$target_page_content = get_field('page_content', $target_post_id);
$parsed_target_content = new DotNotation($target_page_content);
$parsed_target_content->set($fieldmap_string, $image_field_value);
$new_page_content = $parsed_target_content->getValues();
update_field('page_content', $new_page_content, $target_post_id);

Works like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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