in case anyone out there, here’s what worked
// Get field from another post
$fields = acf_get_fields('group_6457959205b1b');
// Update fields in this post
foreach ($fields as $field) {
$field_name = $field['name'];
$field_value = get_field($field_name, $another_post_id);
update_field($field_name, $field_value, $post_id);
}
Thank you for your help.
So far, the result is the same, but I believe you have indicated the direction in which I can move further in order to solve this problem.
In fact, I’m trying to update fields using template, which is another record (I specify it in the record object type field).
And I try to do this while editing.
I managed to achieve a partial solution – text fields change.
However, there is a problem with other field types, it may make sense to specify field types.
Also in the process, I found that in case of a field change – specifying another template record (the record object field) – the current fields are updated based on the record previously stored in this field.
Hello. My thanks for your help in finding a solution to the problem.
The code in this version did not work as expected, but if i change json to JSON, then the text fields change.
Unfortunately, the checkbox and image fields remain unchanged. What could be the problem?
Thanks again. For me, to achieve such a result is already a great progress. I’ve been trying to solve this problem for a week now.
Thank you for reply. If i correct understand, i write this code in funtions.php
add_action( 'wp_ajax_my_acf_update2', function(){
// Get post id
$post_id = $_POST['post_id'];
// Get id another post from the relation field
$another_post_id = get_field('Blocks-FirstSlide-copyBlock', $post_id );
// Getting data
$source_fields = get_field_objects($another_post_id);
//Creating response
$response = array();
foreach ($source_fields as $field) {
$response[$field['key']] = $field['value'];
}
echo json_encode($response);
exit;
});
and this code in script:
jQuery( function ( $ ) {
$ ('.acf-field-64647e4980e03').click( function(){
var input = $(this);
var post_id = $('#post_ID').val();
$.ajax({
type : 'POST',
url : ajaxurl,
data:{
action:'my_acf_update2',
post_id: post_id
},
success: function(data) {
for (var key in json) {
var field = acf.getField(key);
field.val(json[key]);
}
}
});
} );
} );
Result:
Uncaught ReferenceError: json is not defined
at Object.success
About button you give good idea, i change trigger next time.
Can you help me with code above?
Thanks for your reply.
I understand that substituting values in the database and not in the fields, which is the wrong way, it is correct to use then save_post.
But then how to update the fields in the current record? It is necessary to write and add code to everything in the success section. I tried a couple of options but it didn’t work. Can you suggest what command should I use?
I try thic code, but its not working:
jQuery( function ( $ ) {
$ ('.acf-field-64647e4980e03').click( function(){
var input = $(this);
var post_id = $('#post_ID').val();
$.ajax({
type: 'POST',
url: ajaxurl,
data: {
action: 'copy_block_data',
post_id: post_id
},
success: function(response) {
if (response.success) {
acf.do_action('append', response.data);
} else {
alert(response.data);
}
},
error: function(xhr, status, error) {
console.log(error);
}
});
}
);
});
add_action('wp_ajax_copy_block_data', 'copy_block_data_callback');
function copy_block_data_callback() {
$post_id = get_field('Blocks-FirstSlide-copyBlock', $post_id );
if ($post) {
$acf_data = get_fields($post_id);
wp_send_json_success($acf_data);
} else {
wp_send_json_error('Not found');
}
}
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.