Custom Fields
Re-Envisioned
Support General rwmb_after_save_post

This topic contains 10 replies, has 2 voices, and was last updated by  Max@powdersky.com 3 months ago.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Author
    Replies
  • #13039

    Anh Tran
    Keymaster

    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);
    #13047

    Max@powdersky.com
    Participant

    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

    #13052

    Anh Tran
    Keymaster

    Yes, I think so. Not sure where it comes from, since Meta Box doesn’t use JSON at all.

    #13054

    Max@powdersky.com
    Participant

    I think it’s the way WordPres get_post_meta gets the values so might be best if I use rwmb?

    #13055

    Anh Tran
    Keymaster

    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.

    #13056

    Max@powdersky.com
    Participant

    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

    #13085

    Anh Tran
    Keymaster

    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.

    #13090

    Max@powdersky.com
    Participant

    Action code:

    https://ghostbin.com/paste/tnjmm

    Thanks Anh

    #13100

    Anh Tran
    Keymaster

    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.

    #13108

    Max@powdersky.com
    Participant

    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!

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

You must be logged in to reply to this topic.