Support Forum
Hi,
Trying to use: rwmb_{$field_id}_after_save_field
field:
array(
'name' => 'Select Level',
'id' => self::$prefix . 'level',
'type' => 'select',
'placeholder' => 'Please select',
'options' => $select
),
$prefix = 'property'
action not working:
function api_check_post_level_updated($field, $meta_value, $old_level, $post_id ) {
//Current data
$measurements = get_post_meta($post_id, '_property_' . $old_level . '_measurements', true);
$bedrooms = get_post_meta($post_id, '_property_' . $old_level . '_bedrooms', true);
$bathrooms = get_post_meta($post_id, '_property_' . $old_level . '_bathrooms', true);
$currency = get_post_meta($post_id, '_property_' . $old_level . '_currency', true);
$booking_url = get_post_meta($post_id, '_property_' . $old_level . '_booking_url', true);
$features = get_post_meta($post_id, '_property_' . $old_level . '_features', false);
$atnr = get_post_meta($post_id, '_property_' . $old_level . '_atnr', true);
$feature_image = get_post_meta($post_id, '_property_' . $old_level . '_feature_image', false);
$slideshow = get_post_meta($post_id, '_property_' . $old_level . '_slideshow_images', false);
$gallery = get_post_meta($post_id, '_property_' . $old_level . '_images', false);
$floorplans = get_post_meta($post_id, '_property_' . $old_level . '_floorplans', false);
$floors = get_post_meta($post_id, '_property_' . $old_level . '_floors', false);
$facilities = get_post_meta($post_id, '_property_' . $old_level . '_facilities', false);
//Copy old level data to new level
update_post_meta($post_id, '_property_' . $meta_value . '_measurements', $measurements);
update_post_meta($post_id, '_property_' . $meta_value . '_bedrooms', $bedrooms);
update_post_meta($post_id, '_property_' . $meta_value . '_bathrooms', $bathrooms);
update_post_meta($post_id, '_property_' . $meta_value . '_currency', $currency);
update_post_meta($post_id, '_property_' . $meta_value . '_booking_url', $booking_url);
update_post_meta($post_id, '_property_' . $meta_value . '_features', $features);
update_post_meta($post_id, '_property_' . $meta_value . '_atnr', $atnr);
update_post_meta($post_id, '_property_' . $meta_value . '_feature_image', $feature_image);
update_post_meta($post_id, '_property_' . $meta_value . '_slideshow_images', $slideshow);
update_post_meta($post_id, '_property_' . $meta_value . '_images', $gallery);
update_post_meta($post_id, '_property_' . $meta_value . '_floorplans', $floorplans);
update_post_meta($post_id, '_property_' . $meta_value . '_floors', $floors);
update_post_meta($post_id, '_property_' . $meta_value . '_facilities', $facilities);
}
add_action('rwmb__property_level_after_save_field', 'api_check_post_level_updated', 10, 5);
any help would be helpful.
thanks
Hi,
Is the $prefix = property
or _property
? _property
matches the action rwmb__property_level_after_save_field
(In this case, $field_id
is _property_level
).
I see you are trying to copy values from old fields to new fields. Please note that, this action happens after field _property_level
is saved. It doesn't guarantee that it happens before or after other fields are saved.
Ah sorry prefix should be _ property _ no spaces.
Ah ok, is there a way I can achieve what I am trying to do it says hook still passes old/new values?
Thanks
It's nearly impossible to guarantee the order of fields saved. So I'd suggest using rwmb_before_save_post
hook to get all the old values of a post before other fields are updated. Then use rwmb_after_save_post
to update the needed fields.
Thanks Anh,
Sorry whats the best way to pass the fields from rwmb_before_save_post to after_save_post?
3 functions?
Just save it in a temporary variable (a class property if you're using a PHP class, or a global variable if you use normal functions).
Thought so, thanks Anh will give it a go and see how we get on.
Hi Anh,
Got round to trying this and it seems to run a few times?
https://ghostbin.com/paste/6tcft
Thanks for your help again.
Hi Anh,
Still not got it to work all values seem to be passing through but they just aren't saving to the post. Updated code: