I’m trying to update a custom field by hooking into the save-post
action, but for reasons I can’t figure out, it’s not working.
The following function is placed in the theme’s functions.php
:
function save_address_meta() {
$meta = get_post_meta( get_the_ID() );
$address = $meta['address'];
update_post_meta(get_the_ID(), $address, 'test');
}
add_action( 'save_post', 'save_address_meta', 50 );
I’ve tried using pre_post_update
as well, as I understand that save_post
won’t actually fire unless something, other than a custom field, is updated in the post – but no luck with this one either.
I’ve spent a few hours searching for solutions on stackexchange and various other sources online, but just not coming right. This is a dumbed-down version of the original code, but even in this basic state it doesn’t appear to be working.
Basically, I’m trying to get the custom field in question, then update it with a string value.
If I print_r
the $meta
array, the custom field value appears in an array as follows:
[address] => Array ( [0] => 50 Call Lane Leeds LS1 6DT United Kingdom )
I’ve also tried accessing this custom field in the function above using $address = $meta['address'][0]
.
I can echo out the value of the key this way, but if I’m not mistaken, it’s the key I’ll need to reference in order for the string, in the 3rd argument, to update the value as intended.