• I’ve had a function for a while where using Pods Framework I disable the Title field in custom posts so that in the edit screen users can add post titles via Pods meta using wp_insert_post_data (I do this to make form filling of custom post types more coherent and flexible).

    It used to be that publishing the post would update the post title right away but now if I check the list of posts in admin instead there is a (no title) placeholder.

    Only by opening the post again and hitting update will the custom meta be applied and the intended title appears in the list of posts.

    Is this something to do with Gutenberg and if so how do I now force posts to add custom meta to Post Title etc as soon as they are published?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Try using the "rest_after_insert_{$this->post_type}" action to save related post meta when posts are published via Ajax from the block editor. Replace {$this->post_type} with your post type slug.

    Thread Starter perryb

    (@perryb)

    Hi @bcworkz,

    Thanks for the reply, I can’t find much information on rest_after_insert_{$this->post_type}.

    I get that it’s an action but I’m not clear where or when I’d call this in relation to wp_insert_post_data.

    Thread Starter perryb

    (@perryb)

    I managed to get round the issue by using pods_api_pre_save.

    Didn’t get to the bottom of why my previous code had stopped working as expected though.

    Moderator bcworkz

    (@bcworkz)

    Cool, whatever works ??

    FWIW, “wp_insert_post_data” has become unreliable because of how the block editor saves and updates data via REST API calls instead of the procedural PHP the classic editor uses. The “rest_after_insert_{$this->post_type}” is an action that fires after posts are inserted via the API. My thought was to hook both these events to do what you need so your code executes no matter which way a post is published. Invariably one or the other will fire. Should both fire for the same event for some reason, no harm is done since both callbacks are working off the same POSTed data.

    The best documentation that is available on the “rest_after_insert_{$this->post_type}” action exists as inline documentation:
    https://core.trac.www.ads-software.com/browser/tags/5.3/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L609
    You can see what is passed and in what context the action fires, which should be adequate information. There is a formal code reference page for this action, but you don’t really learn anything that’s not evident from examining the source code.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘wp_insert_post_data filter no longer saves custom meta on publish’ is closed to new replies.