Home › Forums › Backend Issues (wp-admin) › update_value hook not working when saving post
Hello All,
I am trying to update a field based off a second field when the second field is updated. IE I have artist_name and artist_sort text fields. When artist_name is updated, I want to automatically create the artist_sort off of the artist_name and update that value.
I setup an update_value hook on name=artist_name, but it is not working when I update the post. it does however work if I update just the 1 field in Admin Columns Pro. Does this hook not work when doing a post update? I then also built a save_post hook, essentially doing the same thing, and it worked in updating the value. I’d prefer not to have to build this into 2 different functions as I have a bunch of these I am trying to do. But I want it to update no matter how the artist_name is being updated. Any thoughts would be appreciated.
Thanks!!
function bidirectional_update_artist_name( $value, $post_id, $field ) {
$field_name = $field['name'];
$field_key = $field['key'];
$artist_name = $value;
$name_sort = name_sort($artist_name);
if (substr_count($name_sort,'%')>0) { $name_sort = $artist_name; }
update_field('artist_sort',$name_sort,$post_id);
return $value;
}
add_filter('acf/update_value/name=artist_name', 'bidirectional_update_artist_name', 10, 3);
function save_post_functions( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
if ( get_post_type( ) == 'artists') {
$artist_name = get_field('artist_name');
$artist_sort = get_field('artist_sort');
$name_sort = name_sort($artist_name);
if (substr_count($name_sort,'%')>0) { $name_sort = $artist_name; }
update_field('artist_sort',$name_sort,$post_id);
}
}
add_action('acf/save_post', 'save_post_functions', 20);
More than likely you are updating the field and then ACF is overwriting it with the submitted value. acf/update_field is called when the field is being updated. So the other field has probably not been updated yet when you update it.
You need to use an acf/save_post filter and do all the work after ACF has finished saving the post https://www.advancedcustomfields.com/resources/acf-save_post/
or you need to update the $_POST[‘acf’][$field_key] value of the field you want to change.
function save_post_functions( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
if ( get_post_type( ) == 'artists') {
$artist_name = get_field('artist_name');
$artist_sort = get_field('artist_sort');
$name_sort = name_sort($artist_name);
if (substr_count($name_sort,'%')>0) { $name_sort = $artist_name; }
if (isset($_POST['acf']['field_XYZ123'])) { // replace with your field key
$_POST['acf']['field_XYZ123'] = $name_sort;
} else {
update_field('artist_sort',$name_sort,$post_id);
}
}
}
add_action('acf/save_post', 'save_post_functions', 20);
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’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.