Let’s say you have this situation (as I did). You are using a custom WordPress Post Type and you have set this custom post type to never publish. You’re using it as a way to save data using only the Dashboard, not ever publish the data to the theme. So the only save function you have is saving as a draft to protect it from public eyes.
Because of this situation the post’s date is always set to the modified date as it is never published. This caused a few issues for me as the application needed to pull up archives for posts based on the creation date, not the modified date.
But WordPress only gives the post a created date if it is published, so saving it as a draft updates post_date
each time.
So, I had to prevent WordPress from updating post_date
each time a draft was saved. I used the below to ensure that on drafts for a specific post type, here ihcrs_events
, only got one post_date
update when it was created, but needed to retain that date going forward.
https://gist.github.com/aubreypwd/c6109ed817ed89e232c3
As you can see we just update the post_date
to the previous value using the wp_insert_post_data filter.
Note, pay extra careful attention to the priority of the filter, 99
. This ensures the last update (there are many on save) retains the date. Using 10
or 20
will not retain the date.