Support Forum
Hi,
Messaged on another topic but as it says resolved I think it's getting missed:
https://support.metabox.io/topic/rwmb_field_id_after_save_field/
What I am trying to do is get the old post meta on (old level) before_save_post and then update the new post meta (new level) with the old data.
https://ghostbin.com/paste/rro3s
Thanks in advanced for your help.
Hi Max,
I think the problem is there are multiple meta boxes and the hooks don't fire properly. They fire per meta box, so if there are 2 meta boxes, the hook rwmb_before_save_post
might fire before one meta box saving action, but after another meta box saving action.
As Meta Box uses hook save_post_{$post_type}
with priority to save the data, please try the replacing the hooks with:
add_action('save_post_{your_post_type}', 'api_get_old_fields', 0);
add_action('save_post_{your_post_type}', 'api_set_new_fields', 20);
Oh thank you so much Anh that has been annoying me for ages!
Another issue though as I am extracting an array and saving it, one of the fields is saving like this:
["View of the Kitzb\u00fcheler Horn Mountain Top","Wellness and Sauna","Open Fireplace","Video Intercom","Modern and Contemporary "]
Is this a json array and can I just use json_decode()?
Thanks
Yes, I think so. Not sure where it comes from, since Meta Box doesn't use JSON at all.
I think it's the way WordPres get_post_meta gets the values so might be best if I use rwmb?
Both rwmb_* functions and WordPress's get_post_meta() don't use JSON at all. They use serialize for arrays. Maybe you need to check how data is saved in your case, just to make sure it's valid. Anyway, as the data is there, you can use json_decode function to decode it.
Hi Anh,
Yeah I tried json decode, I assume when I loop through I just have $value[$i] for the increment and that should save correctly in the DB?
Thanks
You're data is a plain array encoded in JSON, so $value[$i]
is correct to iterate each of the value stored.
I'm not sure how it's saved in the DB, though. But at least it works for now. Please post the code for the meta box/field here for me to see why it saves as json-array.
Hmm, couldn't find anywhere the JSON value is stored.
However, I found something confusing in your code:
This part:
if( is_string($field['value']) && is_array(json_decode($field['value'], true)) ) {
$array = json_decode( $field['value'] );
$i = 0;
foreach( $array as $value) {
update_post_meta($post_id, '_property_' . $new_level . '_' . $field['key'], $value[$i]);
$i++;
}
} else {...}
Here you check if $field['value']
is a json-string. But when you decode it with $array = json_decode( $field['value'] );
, then $array
will be an object! Not sure why the next foreach
loop work.
Sorry dont think it is working as it's not detecting it as an array anyway that was me fiddling but still saving as json array which is odd!