WordPress publish_post hook not getting featured image and meta on first publish, but works on updating title

I don’t know if it’s the Gutenberg editor or if it’s the hook
publish_post

The hook itself works, and if you used the old WordPress post editor, then the issue in question would not happen.

So you can say that it’s the Gutenberg/block editor.

why it’s not returning the meta and featured image

Because Gutenberg uses the REST API, and by the time the publish_post hook is fired (when wp_update_post() is called — see source), the post’s featured image and other meta data have not yet been saved/processed.

How to fix the issue

If you’re using WordPress 5.6 or later, then for what you’re trying to do, you would want to use the wp_after_insert_post hook which works well with the old/classic editor and the Gutenberg/block editor.

Excerpt from https://make.wordpress.org/core/2020/11/20/new-action-wp_after_insert_post-in-wordpress-5-6/:

New action wp_after_insert_post in WordPress 5.6.

The new action wp_after_insert_post has been added to WordPress
5.6 to allow theme and plugin developers to run custom code after a post and its terms and meta data have been updated.

The save_post and related actions have commonly been used for this
purpose but these hooks can fire before terms and meta data are
updated outside of the classic editor. (For example in the REST API,
via the block editor, within the Customizer and when an auto-draft
is created.)

The new action sends up to four parameters:

  • $post_id The post ID has been updated, an integer.
  • $post The full post object in its updated form, a WP_Post object.
  • $updated Whether the post has been updated or not, a boolean.
  • $post_before The full post object prior to the update, a WP_Post object. For new posts this is null.

And here’s an example which mimics the publish_post hook, i.e. the // your code here part below would only run if the post is being published and is not already published (the post status is not already publish):

add_action( 'wp_after_insert_post', 'my_wp_after_insert_post', 10, 4 );
// Note: As of writing, the third parameter is actually named $update and not $updated.
function my_wp_after_insert_post( $post_id, $post, $update, $post_before ) {
    if ( 'publish' !== $post->post_status                           ||
        ( $post_before && 'publish' === $post_before->post_status ) ||
        wp_is_post_revision( $post_id )
    ) {
        return;
    }

    // your code here
}