rwmb_after_save_post
- This topic has 10 replies, 2 voices, and was last updated 6 years, 2 months ago by
[email protected].
-
AuthorPosts
-
January 14, 2019 at 5:06 PM #13023
[email protected]
ParticipantHi,
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.
January 17, 2019 at 9:33 AM #13039Anh Tran
KeymasterHi 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);
January 17, 2019 at 11:22 PM #13047[email protected]
ParticipantOh 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
January 18, 2019 at 4:09 PM #13052Anh Tran
KeymasterYes, I think so. Not sure where it comes from, since Meta Box doesn't use JSON at all.
January 18, 2019 at 4:30 PM #13054[email protected]
ParticipantI think it's the way WordPres get_post_meta gets the values so might be best if I use rwmb?
January 18, 2019 at 5:02 PM #13055Anh Tran
KeymasterBoth 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.
January 18, 2019 at 5:12 PM #13056[email protected]
ParticipantHi 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
January 21, 2019 at 10:03 AM #13085Anh Tran
KeymasterYou'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.
January 21, 2019 at 5:28 PM #13090[email protected]
ParticipantJanuary 22, 2019 at 2:32 PM #13100Anh Tran
KeymasterHmm, 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 nextforeach
loop work.January 22, 2019 at 4:36 PM #13108[email protected]
ParticipantSorry 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!
-
AuthorPosts
- You must be logged in to reply to this topic.