2

I am updating one of my plugins for Gutenberg. I need to check for the presence of a custom post meta field when a post is updated or published, so I've hooked into publish_post:

function process_meta( $ID, $post ) {
    if (get_post_meta($ID, 'my_post_meta_field', true)) {
        // do something
    }
}
add_action('publish_post', 'process_meta', 10, 2);

The problem is that post meta fields have not yet been saved to the database when this hook (or any others that I'm aware of) fires, so unless a post draft has already been saved, my_post_meta_field always appears empty at this point in my code.

In the old version of my plugin, I would simply check the contents of the $_POST[] array. However, Gutenberg now saves content directly via the WordPress API, so there is no POST data to intercept.

How can I work around this problem?

  • Could you expand a little on exactly what are you checking/modifying from the post meta? – Alvaro Jan 31 at 10:14
  • The plugin connects WordPress with forum software. An author can provide an existing forum thread URL in the meta field to associate that post with the thread from the outset. Otherwise, the plugin creates a new thread in the forum programmatically. It needs to know if the meta field has been provided by the user/author before it can determine if it needs to create that thread. – 0Seven Jan 31 at 22:17
  • You can check here, removed filters and actions. As far as I understand from there, the publish_post should work. Could you confirm the post meta is being saved although the action is not triggering? Otherwise a solution could be to trigger those actions inside the editor using JavaScript and the REST API (with the apiFetch package). – Alvaro Jan 31 at 22:47
  • Thanks, I'll look into it. The action is triggered and the post meta is saved to the database. The post meta just hasn't been saved to the database yet when publish_post (or any other hook I can find) is triggered, so it is becoming apparent there is no way to process post meta data during publication with Gutenberg. – 0Seven Feb 1 at 0:42
  • If the meta is correctly saved but using get_post_meta on publish_post action is displaying the wrong value then, I guess it might be a bug. – Alvaro Feb 1 at 11:35
1

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.

  • It worked. But how can I get $_POST data? I have an input field inside an old metabox and I need to grab its data to perform actions (save and trigger other things). – Léo Muniz Mar 31 at 16:32
  • 1
    There is no $_POST sent when using Gutenberg, because it only sends data back to the server via WP API requests. You will need to save that input field data as its own Post Meta data like in my example above, or write a custom WP API endpoint and send your field data to that when it is modified. – 0Seven Apr 1 at 18:42
-1

Replace add_action('publish_post', 'process_meta', 10, 2); with add_action('save_post', 'process_meta', 10, 2);

My Code:

function process_meta( $ID, $post ) {
    // Saving the data for future use
    if ( isset( $_POST['YOUR CUSTOM FIELD NAME'] ) ) {
        update_post_meta( 'my_post_meta_field', $_POST['YOUR CUSTOM FIELD NAME']);
    }
}
add_action('save_post', 'process_meta', 10, 2);

You will access the value in your other PHP or plugin's file like this way get_post_meta( get_the_ID(), 'my_post_meta_field', true ); .

  • I have already tried save_post, but it does not work. – 0Seven Jan 31 at 7:37
  • edited my code. see at above. – Chinmoy Kumar Paul Jan 31 at 7:54
  • 1
    Thanks, but the $_POST[] array is empty when publishing a post via Gutenberg. It transmits all of the post's information back to the server via a request to the WP API. Maybe there is a way to get at that data? – 0Seven Jan 31 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.