I’ve created a wordpress blog locally hosted on a Raspberry Pi 4 4GB using a 500GB USB SSD. It works beautifully as an evernote replacement. I do have a problem with dating the posts however.
I want every post dated as accurately as possible based on original publishing dates of content (when known). For example, a book published in 1886 and made available on the public domain in PDF form I want to be able to post and date for December 31st 1886. WordPress however doesn’t allow dating before 1901.
Is there an override for this that will allow me to use ‘ancient’ dating whenever I prefer to?
This is something totally new to me, so I did some research. What an exciting question!
I had a look at the how WP saves the date when a post is saved. WP runs post date through wp_checkdate()
, which uses PHP native function checkdate()
. According to the PHP docs checkdate()
considers any year between 1 and 32767 valid, so 1800’s shouldn’t be a problem.
I tested the saving behaviour locally and created a post with a date of 1856-09-03. The date got saved correctly to the database, but it wasn’t showing on the dashboard or the public view of the site.
I then look how (get_)the_date()
works, in case you’re using it in your page templates. It basically turns the post_date into unix timestamp, if I understood correctly, which is limited to 1901-2038 as explaned here, https://stackoverflow.com/questions/3985764/display-past-dates-before-the-year-1800. Luckily that very same Q&A also has the solution to the date problem – DateTime
.
Here’s how to replace default date formatting with DateTime
in (get_)the_date()
.
add_filter( 'get_the_date', function($the_date, $d, $post){
$date = new DateTime( $post->post_date );
return $date->format('Y-m-d H:i:s'); // change formatting as needed
}, 10, 3 );
With that filter I got 1856-09-03 showing inside the post loop when using the_date()
.
To get the old dates showing on the posts list, you can do this,
add_filter( 'manage_posts_columns', function($columns){
unset($columns['date']); // remove default date column
$columns['real_date'] = __('Date'); // replace with custom column
return $columns;
});
add_action( 'manage_posts_custom_column' , function( $column, $post_id ){
if ( 'real_date' === $column ) {
global $post;
$date = new DateTime( $post->post_date );
echo $date->format('Y-m-d H:i:s');
}
}, 10, 2 );
add_filter( 'manage_edit-post_sortable_columns', function($columns){
unset($columns['date']);
$columns['real_date'] = __('Date'); // make custom date column sortable
return $columns;
});
To change the post date shown on the post edit screen’s Publish metabox, I think the only option is to use js/jquery as I didn’t notice any filters related to it.
EDIT
And if you want to use the default date formatting you’ve set in the General Settings, you can get it with get_option( 'date_format' );
. This way you don’t need to hardcode the date formats to the filters above, e.g. 'Y-m-d H:i:s'
.