- This topic has 3 replies, 3 voices, and was last updated 2 years, 6 months ago by
Leo.
-
AuthorPosts
-
March 18, 2023 at 4:58 am #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.
March 18, 2023 at 5:51 am #2572068David
StaffCustomer SupportHi 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 thewp_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 ?
March 18, 2023 at 11:06 am #2572476tabl35
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?
March 18, 2023 at 11:11 am #2572478Leo
StaffCustomer SupportBut 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.
-
AuthorPosts
- You must be logged in to reply to this topic.