Hi,
I recently started using CMB2 - so far it's amazing but i've come to a dead end.
A project i'm currently working on requires posts to be created & updated on one site and duplicated across all other sites on the multisite.
Before using CMB2 - this worked fine, but ever since switching to CMB2 non of the fields save the first time around, so I have to save the post once, populate and save again.
My post duplicator runs wp_insert_post() / wp_update_post() on every "save_post" event.
Once i disable this action, CMB2 works as normal.
The documentation isn't entirely clear about this situation.
My presumption is my save_post function takes over the cmb2 save_post?
If this is the case, what would be the best way to run my save_post AFTER cmb2 has finished without having to save the post twice before populating ?
Hope this makes sense.
Thank you
J
Hmmm.
There shouldn't be any "taking over" since CMB2 would be hooking into the same actions, and that just does in order received. No overriding involved unless something is doing a remove_action somewhere.
To change the order, you'd need to find where your code is being executed and lower the priority. Looking at CMB2, we run ours on default 10. add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
You'd want to do 11 or higher to make it run later.
@tw2113 After much head scratching I dived into the CMB2 class and found the following event.
cmb2_{$this->object_type()}_process_fields_{$this->cmb_id}
cmb2_[post]_process_fields_[metabox id]
I just run my function with this event. seems to be doing the job so far, it also allows me to check current post meta with incoming meta - really useful.
Thanks for looking into my ticket.
J
Aweoms to see you managed to work out a solution for this. Thanks for the followup. :)
If you are trying to update a CMB2 field with update_post_meta() in the "save_post_{$post->post_type}" action hook on a CPT, you will find it does not persist.
This action is called before the "save_post" action, so your new value will be over-written by whatever is in the field in the editor form. Might save somebody else a fair bit of debugging.
I'm kind of wondering if the save_post_{$post->post_type} item that twobyte mentions is one where if you return any value other than null to the filter, it hotwires and doesn't proceed further with the default update process. However, I can't manage to track that filter location down just yet to confirm, due to the placeholder aspect.
Nah, it's a normal do_action hook, and it is run before ours.
Ah, it's from core and not CMB2. Carry on.
Most helpful comment
Hmmm.
There shouldn't be any "taking over" since CMB2 would be hooking into the same actions, and that just does in order received. No overriding involved unless something is doing a remove_action somewhere.
To change the order, you'd need to find where your code is being executed and lower the priority. Looking at CMB2, we run ours on default 10.
add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );You'd want to do 11 or higher to make it run later.