• Resolved vallelblanco

    (@vallelblanco)


    I have the following code in a project, that it worked before upgrading to WordPress 5, and what this used to do is to keep the old value on post_modified and post_modified_gmt fields when saving the post and a checkbox (vb_modified_date_switch) is checked in the post editor.

    function vb_post_modified_date_update($data, $postarr) {  
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )   
          return;  
    
        if (!empty($postarr['vb_modified_date_switch'])) {  
            $data['post_modified'] = $postarr['post_modified'];  
            $data['post_modified_gmt'] = $postarr['post_modified_gmt'];  
        }  
    
        return $data;  
    }  
    
    add_filter( 'wp_insert_post_data', 'vb_post_modified_date_update', '99', 2 );

    According to the documentation of wp_insert_post_data, $data should have the post data and $postarr should have the unmodified post data. However, when debuging, I found that both variables have the same post data (modified data), so now I cant get the original post_modified and post_modified_gmt.

    Is there any other way I can change the post data before is saved? Is there an updated documentation?

    Thanks

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

    (@bcworkz)

    G’berg updates posts via the REST API. Unverified, but what I think is going on is by the time your callback runs when the user explicitly updates, the “old” last modified date is from seconds ago when G’berg last updated via the API. I don’t suppose you’d want to use the original insertion date for this?

    Probably not, not the same thing at all. You could try hooking into the API process used by G’berg and try to maintain the original date that way. Or perhaps include the initial modified date in a hidden field with your checkbox. Then when checked, use this field as the source instead of the passed data.

    Thread Starter vallelblanco

    (@vallelblanco)

    @bcworkz You are right, I noticed there is an ajax call that saves the post and then makes the normal page post.

    Using the hidden field sounds like a good idea and very easy.

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘wp_insert_post_data not working correctly on WordPress 5 (Gutenberg)’ is closed to new replies.