Custom Fields
Re-Envisioned
Support General rwmb_{$field_id}_after_save_field

This topic contains 8 replies, has 2 voices, and was last updated by  Max@powdersky.com 6 months, 1 week ago.

  • Creator
    Topic
  • #12580
    Resolved
    Max@powdersky.com
    Participant

    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

Viewing 8 replies - 1 through 8 (of 8 total)
  • Author
    Replies
  • #12618

    Anh Tran
    Keymaster

    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.

    #12620

    Max@powdersky.com
    Participant

    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

    #12621

    Anh Tran
    Keymaster

    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.

    #12623

    Max@powdersky.com
    Participant

    Thanks Anh,

    Sorry whats the best way to pass the fields from rwmb_before_save_post to after_save_post?

    3 functions?

    #12644

    Anh Tran
    Keymaster

    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).

    #12646

    Max@powdersky.com
    Participant

    Thought so, thanks Anh will give it a go and see how we get on.

    #12780

    Max@powdersky.com
    Participant

    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.

    #12973

    Max@powdersky.com
    Participant

    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:

    https://ghostbin.com/paste/rro3s

Viewing 8 replies - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.