Site logo

[Support request] Prevent WordPress from Updating the Modified Date

Home Forums Support [Support request] Prevent WordPress from Updating the Modified Date

Home Forums Support Prevent WordPress from Updating the Modified Date

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2572002
    tabl35

    I am using the GP post meta template, GP headline block and dynamic data, displaying the updated date instead of the published date. What I don’t like is that the updated date constantly changes, even when only minor changes are made to the post, such as adding a category or a tag to a post etc.

    I found this snippet on the web, which is supposed to prevent WordPress from updating the modified date unless changes are made to the title of the post:

    add_filter( 'wp_insert_post_data', 'do_not_change_modified_date', 10, 2 );         
    function do_not_change_modified_date( $data, $postarr ) {
        
        // return if post ID is zero
    
        // this indicates that a new post is being processed
    
        if( ((int)$postarr['ID'] === 0) || !$postarr['ID']) return $data;
        
        // get the post
    
        $post_befor_update = get_post($postarr['ID']);
        
        // return if the modified date is not set
    
        // this happens in revisions and can heppen in other post types
    
        if( !isset($data['post_modified'])              || 
            !isset($data['post_modified_gmt'])          || 
            !isset($post_befor_update->post_modified)   || 
            !isset($post_befor_update->post_modified_gmt)) {
            return $data;
        }
        
        // return if changes were made to the content or title
    
        if((wp_unslash($data['post_title'])    !== $post_befor_update->post_title)) return $data;
        
        // change the modified date back to how it was before the update
    
        $data['post_modified']      = $post_befor_update->post_modified;
        $data['post_modified_gmt']  = $post_befor_update->post_modified_gmt;
    
        return $data;
    }

    Is this save to use long-term? I would add it with code snippets and let it run permanently.

    #2572068
    David
    Staff
    Customer Support

    Hi there,

    i cannot say if its good to use that long term or if it is a valid code.
    you can see in the codex what the wp_insert_post_data filter does:

    https://developer.wordpress.org/reference/hooks/wp_insert_post_data/

    Its filtering data before it gets written to your database.
    So that means you lose track of any updated times …..

    Do you simply want to NOT show the modified date on the front end ?

    #2572476
    tabl35

    No, I want to show the modified date on posts that received a significant update. But I don’t want every change made to a post to be reflected in the modified date. For example fixing a minor spelling error shouldn’t change the date on 10k word post.

    Ideally you’d be able to manually change the modified date displayed on the frontend. This code snippet seems to work as intended, but I am worried that it might mess up something else. When you say “you lose track of any updated times”, does it mean I won’t be able to see any revisions made to a post?

    #2572478
    Leo
    Staff
    Customer Support

    But I don’t want every change made to a post to be reflected in the modified date. For example fixing a minor spelling error shouldn’t change the date on 10k word post.

    Unfortunately this is 100% handled by WordPress and not something the theme can control.

    This code snippet seems to work as intended, but I am worried that it might mess up something else.

    You would just have to test the solution and monitor the result.

    When you say “you lose track of any updated times”, does it mean I won’t be able to see any revisions made to a post?

    I believe so – you should be able to test it out in a staging site though.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.