The solution is to use a different hook which fires after post meta data has been saved by Gutenberg to the WP API. After reading this related discussion on Github and inspecting this code by n7studios, I discovered that rest_after_insert_post
is a hook to use.
So, the way to reliably check for the existence of a WordPress Post Meta field when using Gutenberg:
function read_my_meta_field( $post, $request ) {
$my_meta_field = get_post_meta($post->ID, 'my_post_meta_field', true);
// Process $my_meta_field as desired...
}
add_action('rest_after_insert_post', 'read_my_meta_field', 10, 2);
Note that you can handle meta fields from other post types by simply changing post
in the first parameter to the add_action()
function, so instead of using 'rest_after_insert_post'
you would use 'rest_after_insert_whatever'
.
Finally, here is a more complete answer to my own question, which checks the meta field only on post publication.
function run_on_post_publication( $ID, $post ) {
// Only register this hook if we really are publishing a new post
add_action('rest_after_insert_post', 'read_my_postmeta_field', 10, 2);
}
add_action('publish_post', 'run_on_post_publication', 10, 2);
function read_my_postmeta_field( $post, $request ) {
$my_meta_field = get_post_meta( $post->ID, 'my_post_meta_field', true);
// Process $my_meta_field as desired...
}
For reference, if you want more control over when, exactly, your code runs, check out the ‘transition_post_status’ hook.