This took me quite a while to figure out, but I finally determined what the proper hooks for managing custom fields in a custom post type in WordPress are.
In my scenario, I have a custom post type that uses the
add_meta_box() function to add a few custom fields. As a matter of fact, those are the only fields available… the title, body, etc, have all been removed. Furthermore, I save those custom fields in a new table, because I need to perform additional actions outside of WordPress with that data, and storing them separately will make that a lot easier.
WordPress allows you to perform four actions with any post type, which meant I needed four hooks in order to keep the data in my table in sync with the status of the post. Those actions are: saving, moving something to the trash, restoring it from the trash, and permanently deleting.
It seems that WordPress is a bit inconsistent in the naming convention of those hooks, which is why it took some trial and error, but after spending some time with it, I’ve tested and confirmed the following four hooks to be the ones you need:
Hooks that didn’t act the way I expected were:
untrash_custom-post-type. For instance,
trash_post only worked with built-in post types, such as pages and posts, but
save_post works even with custom post types.
Hopefully this two minute blog post will save someone some time, because those four hooks are pretty much necessary if you’re working with custom fields (unless you disable trash entirely and just permanently delete).