I am trying to update a Custom Field when we add a new related post.
1. Add a Post of type "parent"
2. Save the post of type "parent"
3. Now add a new Child Post, while inside the "parent" post edit screen, using the Toolset "Related Posts" Metabox
4. In the related post, there is a Custom Field.
5. That custom field should be updated programmatically.
It works just fine when I update the related post while inside the related post edit screen.
When I however add the post from within the Related posts Metabox, the field is not updated.
I tried Several code approaches.
The one that works while inside the related post edit screen (save_post at a late priority), does not work.
Then I also tried to hook into updated_post_meta, as well at a late priority, and also it does not work.
I tried at PHP_INT_MAX priority, without luck.
What is the correct hook or code to update a Custom Field when we save the related post from the Related posts Metabox?
Attempted code:
1. Works just fine when inside the related post edit screen:
2. Works as well if hooked to updated_post_meta (if inside the related post edit screen)
3. Neither works if we add the post from within the related post Metabox.
==> All hooks have been tried as well with PHP_INT_MAX
I'm nosing around in the source code and it looks like the related post is created/updated, some code happens, and then the field values are set.
So if you try to use a save_post hook or anything relating to updating the post itself to trigger your code which sets up the post meta, then whatever you did is going to be overwritten a few lines of code later.
You can only use the hooks related to updating the post meta (e.g. update_{$meta_type}_meta, update_postmeta, or updated_{$meta_type}_meta and updated_postmeta).
If you place a breakpoint at types/vendor/toolset/types/embedded/classes/field.php:386 then you see when the saving of the custom field actually occurs.
Hopefully that's enough to put you on the right track, but let me know if not...
I recall as well that in past while debugging I saw some weird stuff going on in the code (actually Toolset copies, deletes and then re-saves all meta each time you save a post, for some WPML compatibility issue, if I recall it right).
I have also hooked the updated_post_meta to a very late (PHP_INT_MAX) priority, and still I don't get the field saved.
However, what I see when I log what field is updated on updated_post_meta...
it shows me only ONE of the fields that I actually have in the related post Metabox!
And ... it is only the field I do NOT set when I add the related post.
The one I want to update programmatically. All other fields are not logged during the updated_post_meta hook, at any priority.
And, they are also not available from get_post_meta at this point, so it is impossible for me to get the value from the database, and update that field which is updated (even if I do NOT update or set that field during adding the related post...)
Plus, an _edit_lock field (if I do a very high priority). Otherwise, nothing there.
That makes no sense to me, it shouldn't be that _not set_ fields get logged, but the set fields don't, and are not even in the get_post_meta at this point?
Did you get a hook to work that takes values from Field 1 and puts them into field 2, when field 2 is NOT set during adding the related post, in a related posts Metabox?
Going a little deeper down the rabbit hole I found custom hooks that Types fires after saving the custom fields, and in the quick tests I ran the hooks do get triggered from the related posts UI, whether or not you supply a value for the custom fields, so I think you should be able to use those.
Here are how the hooks are set up in types/vendor/toolset/types/embedded/classes/field.php:461 (so you can see the parameters available):