$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>block editor - How do I access post meta data when publishing a new post in Gutenberg?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

block editor - How do I access post meta data when publishing a new post in Gutenberg?

matteradmin9PV0评论

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?

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?

Share Improve this question asked Jan 31, 2019 at 7:34 0Seven0Seven 612 silver badges6 bronze badges 5
  • Could you expand a little on exactly what are you checking/modifying from the post meta? – Alvaro Commented Jan 31, 2019 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 Commented Jan 31, 2019 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 Commented Jan 31, 2019 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 Commented Feb 1, 2019 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 Commented Feb 1, 2019 at 11:35
Add a comment  | 

2 Answers 2

Reset to default 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.

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 ); .

Post a comment

comment list (0)

  1. No comments so far